0

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?

Lin Du
  • 88,126
  • 95
  • 281
  • 483
Michael
  • 6,823
  • 11
  • 54
  • 84
  • Are you sure you defined `GetExternalConfluencePage` type in the `.graphql` file at the second location rather than use it? We can't reproduce your error without seeing the code of `.graphql` file – Lin Du Jan 05 '21 at 08:38
  • Thx. I added the .graphql file in the question. – Michael Jan 05 '21 at 08:49
  • Does local schema use the input type `GetExternalConfluencePage` defined in common schema? – Lin Du Jan 05 '21 at 09:11
  • That's probably were I lack understanding (I'm a beginner in graphQL). I thought that once defined in any .graphql file, I can use an input definition everywhere. I tried to move the externalConfluencePage definition into the common .graphql file using the "extend type Query" command, but then the error message "Error: Query.externalConfluencePage defined in resolvers, but not in schema" points to the same problem. I have to figure out how I can define an extra Query field in a module and use it locally. You have a hint for me? – Michael Jan 05 '21 at 09:42

1 Answers1

2

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",
Lin Du
  • 88,126
  • 95
  • 281
  • 483