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

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)

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 3
  • Comments: 23

Most upvoted comments

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.

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

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!

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.

@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!
}