Marcelo Lewin
Marcelo Lewin

Reputation: 21

How can I break apart a GraphQL Schema into multiple files in graphql-yoga

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!

Upvotes: 2

Views: 4140

Answers (1)

xadm
xadm

Reputation: 8418

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

Upvotes: 1

Related Questions