2

I'm new to GraphQL. I'm trying to break apart a schema.sql file into multiple files.

First question, is this a standard way of doing things with GraphQL (for example, Query.graphql, Mutation.graphql, TypeA.graphql, etc.)

Second, I thought the parameter in typeDefs (for graphql-yoga) allows me to pass an array of locations for all the schema files, but it's not working for me (See below after the code for the error I'm geting.

Here is my code in my main index.js file:

import { graphQLServer, GraphQLServer } from 'graphql-yoga';
import db from '../database/db';
import Query from './resolvers/Query';
import Mutation from './resolvers/Mutation';
import Post from './resolvers/Post';
import User from './resolvers/User';
import Comment from './resolvers/Comment';
const typeDefs = [
     './schemas/query.graphql', 
     './schemas/mutation.graphql',
     './schemas/user.graphql'
];

const server = new GraphQLServer({
    typeDefs: typeDefs,
    context: { db },
    resolvers: { Query, Mutation, Post, User, Comment }
});

server.start(()=> {
    console.log("Server started on localhost:4000!");
});

Here is the error I'm getting:

Error: Field createUser: Couldn't find type User in any of the schemas.
    at collectNode (C:\Websites\Learning-GraphQL - Local Copy\graphql-bootcamp\graphql-basics\node_modules\graphql-import\src\definition.ts:154:15)    
    at C:\Websites\Learning-GraphQL - Local Copy\graphql-bootcamp\graphql-basics\node_modules\graphql-import\src\definition.ts:135:7
    at Array.forEach (<anonymous>)
    at collectNewTypeDefinitions (C:\Websites\Learning-GraphQL - Local Copy\graphql-bootcamp\graphql-basics\node_modules\graphql-import\src\definition.ts:134:26)
    at Object.completeDefinitionPool (C:\Websites\Learning-GraphQL - Local Copy\graphql-bootcamp\graphql-basics\node_modules\graphql-import\src\definition.ts:49:39)
    at Object.importSchema (C:\Websites\Learning-GraphQL - Local Copy\graphql-bootcamp\graphql-basics\node_modules\graphql-import\src\index.ts:127:18) 
    at mergeTypeDefs (C:\Websites\Learning-GraphQL - Local Copy\graphql-bootcamp\graphql-basics\node_modules\graphql-yoga\src\index.ts:456:14)
    at C:\Websites\Learning-GraphQL - Local Copy\graphql-bootcamp\graphql-basics\node_modules\graphql-yoga\src\index.ts:472:32
    at Array.reduce (<anonymous>)
    at mergeTypeDefs (C:\Websites\Learning-GraphQL - Local Copy\graphql-bootcamp\graphql-basics\node_modules\graphql-yoga\src\index.ts:471:21)

As you can see, it says it can't find one of my types (User), but I do have it in the User.graphql schema file. When I have everything in just one file and pass that file only as the typeDefs, it works fine. Thanks in advance for your help!

2
  • Saying something isn't working isn't particularly helpful -- if you're seeing an error, you should edit your question to include the full error message. Passing in an array of paths should work just fine, but you probably need absolute file paths, not relative ones, since your code is compiled. Commented Feb 6, 2020 at 17:34
  • Sorry about that. I'm new to Stack Overflow. I've added more detail. I've tried absolute paths, but it still fails with the error listed above. Commented Feb 6, 2020 at 18:59

1 Answer 1

1
const typeDefs = [  
  './schemas/query.graphql',   
  './schemas/mutation.graphql',  
  './schemas/user.graphql'  
];

this is only an array of strings

from docs:

typeDefs - String or Function or DocumentNode or array of previous

you can make something like:

import Query from './schemas/query.graphql'
import Mutation from './schemas/mutation.graphql'
import User from './schemas/user.graphql'

const typeDefs = [Query, Mutation, User];

config required: https://github.com/apollographql/graphql-tag#webpack-preprocessing-with-graphql-tagloader

Sign up to request clarification or add additional context in comments.

4 Comments

Unfortunately, that didn't work. I'm getting parsing errors from the file. I'm thinking it's because it's thinking it's a JS file?
try this method: medium.com/@choudlet/…
the first method is from github.com/sysgears/apollo-universal-starter-kit/blob/… - rootSchemaDef imported from raw .graphql file into array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.