typescriptgraphqlgraphql-tools

How to load a GraphQL schema from two source locations with graphql-tools?


I want to load graphql schemas from different locations (local app and a common module). I do:

export function GraphqlServer(
    resolvers: Record<string, IResolvers>,
    config?: Config
) {
    const localScheme = '**/*.graphql';
    const commonSchema = join(
        __dirname,
        './packages/common/src/graphql/api/**/*.graphql'
    );
    const schema = loadSchemaSync([localScheme, commonSchema], {
        loaders: [new GraphQLFileLoader()]
    });

    const schemaWithResolvers = addResolversToSchema({
        schema,
        resolvers: {
            ...resolvers,
            Date,
            DateTime,
            JSON
        }
    });

But it seems that only the schemas of the first location are loaded. I get an error

Error: Unknown type "GetExternalConfluencePage".

which is defined in a .graphql file at the second location. If I move this file from the second location to the first than it works.

The content of the .graphql file is:

input GetExternalConfluencePage {
    confluencePageId: String!
}

type ExternalConfluencePage {
    title: String!
    body: String!
}

The .graphql file in the first location contains:

type Query {
    macro(input: GetMacroInput!): Macro!
    externalConfluencePage(
        input: GetExternalConfluencePage!
    ): ExternalConfluencePage
}

Any hint what could be the problem?


Solution

  • It works just fine.

    E.g.

    index.ts

    import { printSchema } from 'graphql';
    import { loadSchemaSync, GraphQLFileLoader } from 'graphql-tools';
    import { join } from 'path';
    
    const localScheme = join(__dirname, './local.graphql');
    const commonSchema = join(__dirname, './common/*.graphql');
    const schema = loadSchemaSync([localScheme, commonSchema], {
      loaders: [new GraphQLFileLoader()],
    });
    
    console.log(printSchema(schema));
    

    common/common.graphql:

    input GetExternalConfluencePage {
      confluencePageId: String!
    }
    
    type ExternalConfluencePage {
      title: String!
      body: String!
    }
    

    local.graphql:

    type Query {
      externalConfluencePage(input: GetExternalConfluencePage!): ExternalConfluencePage
    }
    

    Print the loaded GraphQL schema to console:

    input GetExternalConfluencePage {
      confluencePageId: String!
    }
    
    type ExternalConfluencePage {
      title: String!
      body: String!
    }
    
    type Query {
      externalConfluencePage(input: GetExternalConfluencePage!): ExternalConfluencePage
    }
    

    package versions:

    "graphql": "^15.4.0",
    "graphql-tools": "^6.2.3",