Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When use string file path for typeDefs,there is a error: Cannot parse the unexpected character "/". #362

Closed
rumsky opened this issue Jun 8, 2018 · 23 comments

Comments

@rumsky
Copy link

rumsky commented Jun 8, 2018

const server = new GraphQLServer({
    typeDefs: './src/schema.graphql',
    resolvers,
})

When use string file path for typeDefs,there is a error:
/Users/harry/reactworkspace/graphql-yoga-server/hackernews-node/node_modules/graphql/language/lexer.js:302
throw (0, _error.syntaxError)(source, pos, unexpectedCharacterMessage(code));
^
GraphQLError: Syntax Error: Cannot parse the unexpected character "/".
at syntaxError (/Users/harry/reactworkspace/graphql-yoga-server/hackernews-node/node_modules/graphql/error/syntaxError.js:24:10)
at readToken (/Users/harry/reactworkspace/graphql-yoga-server/hackernews-node/node_modules/graphql/language/lexer.js:302:32)
at Object.lookahead (/Users/harry/reactworkspace/graphql-yoga-server/hackernews-node/node_modules/graphql/language/lexer.js:61:43)
at Object.advanceLexer [as advance] (/Users/harry/reactworkspace/graphql-yoga-server/hackernews-node/node_modules/graphql/language/lexer.js:52:33)
at expect (/Users/harry/reactworkspace/graphql-yoga-server/hackernews-node/node_modules/graphql/language/parser.js:1296:11)
at parseDocument (/Users/harry/reactworkspace/graphql-yoga-server/hackernews-node/node_modules/graphql/language/parser.js:107:3)
at Object.parse (/Users/harry/reactworkspace/graphql-yoga-server/hackernews-node/node_modules/graphql/language/parser.js:38:10)
at getDocumentFromSDL (/Users/harry/reactworkspace/graphql-yoga-server/hackernews-node/node_modules/graphql-import/dist/index.js:107:26)
at Object.importSchema (/Users/harry/reactworkspace/graphql-yoga-server/hackernews-node/node_modules/graphql-import/dist/index.js:59:20)
at mergeTypeDefs (/Users/harry/reactworkspace/graphql-yoga-server/hackernews-node/node_modules/graphql-yoga/dist/index.js:402:37)

@Robert-Janagap
Copy link

let me see your schema.graphql

@Damilare1
Copy link

Please I am having similar challenges, has the issue been resolved. If it has, how was it resolved?

@AviIhej
Copy link

AviIhej commented Nov 5, 2018

I am having the same issue.. has anyone figured it out?

@eveshi
Copy link

eveshi commented Dec 12, 2018

same issue as well, has the problem solved?

@chinadbo
Copy link

same

@Esom
Copy link

Esom commented Feb 2, 2019

quite new to graphql but this error happens when this file "./src/schema.graphql" is empty ..a type must be created & also a mutation & resolver

@jackgray
Copy link

jackgray commented Feb 8, 2019

I am getting this error and it seems to have something to do with reading the token although I am using syntax defined in the prisma-auth example

here's the full output:

Debugger listening on ws://127.0.0.1:9229/b85422b9-0b37-4cee-a743-8bfc5882fccf
For help, see: https://nodejs.org/en/docs/inspector

/Users/jackgray/webapps/govtrackr/server/node_modules/graphql/language/lexer.js:316
throw (0, _error.syntaxError)(source, pos, unexpectedCharacterMessage(code));
^
GraphQLError: Syntax Error: Cannot parse the unexpected character ".".
at syntaxError (/Users/jackgray/webapps/govtrackr/server/node_modules/graphql/error/syntaxError.js:24:10)
at readToken (/Users/jackgray/webapps/govtrackr/server/node_modules/graphql/language/lexer.js:316:32)
at Object.lookahead (/Users/jackgray/webapps/govtrackr/server/node_modules/graphql/language/lexer.js:62:43)
at Object.advanceLexer [as advance] (/Users/jackgray/webapps/govtrackr/server/node_modules/graphql/language/lexer.js:52:33)
at expect (/Users/jackgray/webapps/govtrackr/server/node_modules/graphql/language/parser.js:1441:11)
at many (/Users/jackgray/webapps/govtrackr/server/node_modules/graphql/language/parser.js:1512:3)
at parseDocument (/Users/jackgray/webapps/govtrackr/server/node_modules/graphql/language/parser.js:115:18)
at Object.parse (/Users/jackgray/webapps/govtrackr/server/node_modules/graphql/language/parser.js:50:10)
at getDocumentFromSDL (/Users/jackgray/webapps/govtrackr/server/node_modules/graphql-import/dist/index.js:119:26)
at collectDefinitions (/Users/jackgray/webapps/govtrackr/server/node_modules/graphql-import/dist/index.js:175:20)

prisma generated just fine so I don't think it has anything to do with the schema or datamodel

utils.js file from the prisma-auth example:


const { verify } = require('jsonwebtoken');

class AuthError extends Error {
	constructor() {
		super('Not authorized');
	}
}

function getUserId(context) {
	const Authorization = context.request.get('Authorization');
	if (Authorization) {
		const token = Authorization.replace('Bearer ', '');
		const verifiedToken = verify(token, process.env.AUTH_SECRET);
	
	return verifiedToken && verifiedToken.userId;
	}
}

module.exports = {
	getUserId
};

and the index.js:


require('dotenv').config();

const { GraphQLServer } = require('graphql-yoga');
const { prisma } = require('./generated/prisma-client');
const { resolvers } = require('./resolvers');
const { permissions } = require('./permissions');
const { getUserId } = require('./utils');

const dev = process.env.NODE_ENV !== 'production';

const server = new GraphQLServer({
	typeDefs: 'src/schema.graphql',
	resolvers,
	middlewares: [ permissions ],
	context: (request) => {
		return {
			...request,
			prisma
		};
	}
});

// server.express.use(compression());
// server.express.use(helmet());
// server.express.use(cookieParser());

if (!dev) {
	server.express.set('trust proxy', 1);
}

const cors = {
	origin:
		dev ? process.env.CLIENT_DEV_URL :
		process.env.CLIENT_PROD_URL,
	credentials: true
};

// option object for graphql yoga
const port = 4000;
const options = {
	port,
	cors
};

server.start(options, ({ port }) =>
	console.log(`Server started, listening on port ${port} for [${process.env.NODE_ENV}]`)
);

@jackgray
Copy link

jackgray commented Feb 8, 2019

wondering how to get a more useful error output... vs code usually catches syntax errors in code so I don't think it's that

@Esom
Copy link

Esom commented Feb 8, 2019

can i see the content of schema.graphql .. is it an empty file? if yes then it shouldn't be

@jackgray
Copy link

jackgray commented Feb 9, 2019

No, and I have also imported everything from my prisma-client. prisma generate would not run with an empty file. Prisma deploys just fine.

here it is:

# import * from "./generated/prisma-client/prisma-schema"

type Query {
	info: String!
	currentUser: User
	allUsers: [User!]
	feed(filter: String, skip: Int, first: Int, orderBy: CommentOrderByInput): Feed!
	filterComments(searchString: String): [Comment!]!
	comment(id: ID!): Comment!
}

type Mutation {
	updateUser(name: String, email: String, password: String, newPassword: String!): User!
	commentBill(billId: ID!, content: String): Comment!
	publishComment(id: ID!): Comment
	deleteComment(id: ID!): Comment
	publish(id: ID!): Comment
	signup(email: String!, password: String!, name: String): AuthPayload!
	login(email: String!, password: String!): AuthPayload!
	likeComment(commentId: ID!): Like!
	likeBill(billId: ID!): Like
}

type Subscription {
	newComment: Comment!
	newLike: Like!
}

type User {
	id: ID!
	email: String!
	name: String
	comments: [Comment]
	likes: [Like]
}

type AuthPayload {
	token: String!
	user: User!
}

type BillFeed {
	bills: [Bill]
	count: Int
}

type CommentFeed {
	comments: [Comment!]!
	count: Int!
}

@jackgray
Copy link

jackgray commented Feb 9, 2019

It seems to have something to do with parsing the token

@jblevins1991
Copy link

Any updates to this? I really need to get my graphql server up and running.

@tiagobnobrega
Copy link

Same here... that's strange. Maybe node version related. I worked around it by reading file contents and passing it to typedefs option, as if it was inlined in the code. Also I'm using ".graphqls" etension.

const typeDefs = fs.readFileSync('./src/schema.graphqls','utf8');
...
const server = new GraphQLServer({
  typeDefs,
  resolvers
});

I suspect it's failing to reconize the argument as as filepath for some reason, so it tries to parse the path as actual graphql schema.
Not why this happens though, I tried the same path for both fs.readFileSync and typeDefs option.
I also tried the full path, but no luck either.

@Shaderzero
Copy link

I managed to solve problem:

const server = new GraphQLServer({
  typeDefs: './src/graph.graphql',
  resolvers
})

And in file graph.graphql first line should be:

schema {
    query: Query
    mutation: Mutation
}

@AmrAbdalrahman
Copy link

same issue with me!
@Shaderzero try you code still not works.

@Shaderzero
Copy link

Shaderzero commented Mar 16, 2019

@AmrAlmagic
Problem is not in code for typeDefs: './src/graph.graphql'
for GraphQLServer important to have proper schema description in this file
my current index,js

import {GraphQLServer, PubSub} from "graphql-yoga";
import db from './db';
import Comment from './resolvers/comment';
import Mutation from './resolvers/mutation';
import Subscription from './resolvers/subscription';
import Post from './resolvers/post';
import Query from './resolvers/query';
import User from './resolvers/user';

const pubsub = new PubSub();

const server = new GraphQLServer({
  typeDefs: './src/schema.graphql',
  resolvers: {
    Comment,
    Mutation,
    Subscription,
    Post,
    Query,
    User
  },
  context: {
    db,
    pubsub
  }
});

server.start(() => {
  console.log('the server is up')
})

and schema.graphql

schema {
    query: Query
    mutation: Mutation
}

type Query {
    users(query: String): [User!]!
    posts(query: String): [Post!]!
    me: User!
    post: Post!
    comments: [Comment!]!
}

type Mutation {
    createUser(data: CreateUserInput!): User!
    deleteUser(id: ID!): User!
    updateUser(id: ID!, data: UpdateUserInput!): User!
    createPost(data: CreatePostInput!): Post!
    deletePost(id: ID!): Post!
    updatePost(id: ID!, data: UpdatePostInput!): Post!
    createComment(data: CreateCommentInput!): Comment!
    deleteComment(id: ID!): Comment!
    updateComment(id: ID!, data: UpdateCommentInput!): Comment!
}

type Subscription {
    comment(postId: ID!): CommentSubscriptionPayload!
    post: PostSubscriptionPayload!
}

input CreateUserInput {
    name: String!
    email: String!
    age: Int
}

input UpdateUserInput {
    name: String
    email: String
    age: Int
}

input CreatePostInput {
    title: String!
    body: String!
    published: Boolean!
    author: ID!
}

input UpdatePostInput {
    title: String
    body: String
    published: Boolean
}

input CreateCommentInput {
    text: String!
    author: ID!
    post: ID!
}

input UpdateCommentInput {
    text: String
}

type User {
    id: ID!
    name: String!
    email: String!
    age: Int,
    posts: [Post!]!
    comments: [Comment!]!
}

type Post {
    id: ID!
    title: String!
    body: String!
    published: Boolean!
    author: User!
    comments: [Comment!]!
}

type Comment {
    id: ID!
    text: String!
    author: User!
    post: Post!
}

enum MutationType {
    CREATED
    UPDATED
    DELETED
}

type PostSubscriptionPayload {
    mutation: MutationType!
    data: Post!
}

type CommentSubscriptionPayload {
    mutation: MutationType!
    data: Comment!
}

@AmrAbdalrahman
Copy link

@tiagobnobrega
I tried your solution works with me however I also tried to upgrade node version still get the same error.
@Shaderzero
my code is here
please inform me if I make something wrong!

@AmrAbdalrahman
Copy link

I solved it !!.
My editor is webstorm and uses graphql plugin. it makes the extension for the schema with (graphqls) with change the extension to (graphql) works fine now.
@Shaderzero thanks for your attention.

@stale
Copy link

stale bot commented May 24, 2019

Due to inactivity of this issue we have marked it stale. It will be closed if no further activity occurs.

@stale stale bot added the status/stale label May 24, 2019
@stale
Copy link

stale bot commented May 31, 2019

Hey 👋, It seems like this issue has been inactive for some time. In need for maintaining clear overview of the issues concerning the latest version of graphql-yoga we'll close it.
Feel free to reopen it at any time if you believe we should futher discuss its content. 🙂

@stale stale bot closed this as completed May 31, 2019
@walame
Copy link

walame commented Aug 13, 2019

Hello, new to GraphQL but was able to figure this out.

Basically, I had a javascript syntax comment on schema.graphql file:

// server/src/schema.graphql

type Query {
...

Once I removed the comment, the error was gone. I guess the // threw it off!

@nbhankes
Copy link

nbhankes commented Jul 14, 2020

This solved a similar issue for me:

const server = new GraphQLServer({
  typeDefs: __dirname + "/schema.graphql",
  resolvers,
});

@Urigo
Copy link
Collaborator

Urigo commented Mar 29, 2022

Hey, @Urigo from The Guild here!

You might know us from projects such as graphql-code-generator, envelop or graphql-tools.

For a long time we thought that the Javascript ecosystem is still missing a lightweight cross-platform, but still highly customizable GraphQL Server.

In the past the awesome Prisma team took on that great challenge and now we are happy to announce that we are continuing them and just released GraphQL Yoga 2.0 - Build fast, extensible, and batteries-included (Subscriptions, Serverless, File uploads support) GraphQL APIs in Node.js 🚀

We have been working a long time on version 2.0 and have been using it in our clients projects for a few months now and shared a couple of alpha cycles here.
Thank you all for your feedback and suggestions, you made this release possible!

Please try Yoga out again, give us feedback and help us spread the word on the new release!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests