ardatan / graphql-import

Import & export definitions in GraphQL SDL
868 stars 56 forks source link

[question] migrate from graphql-import to graphql-tools with graphql's `buildSchema` #601

Closed francoisromain closed 4 years ago

francoisromain commented 4 years ago

Hello,

I have seen the migration instructions to graphql-tools, but my config is a bit different.

currently I have

import { buildSchema } from 'graphql'
import { importSchema } from 'graphql-import'
import { join } from 'path'

const index = importSchema(join(__dirname, 'schemas/index.graphql'))

const schema = buildSchema(index)

export default schema

I tried

import { buildSchema } from 'graphql'
import { loadSchemaSync } from '@graphql-tools/load'
import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader'
import { join } from 'path'

const index = loadSchemaSync(join(__dirname, 'schemas/index.graphql'), {
  loaders: [new GraphQLFileLoader()],
})

const schema = buildSchema(index)

export default schema

But there is a typescript error

const index: GraphQLSchema
Argument of type 'GraphQLSchema' is not assignable to parameter of type 'string | Source'.
  Type 'GraphQLSchema' is missing the following properties from type 'Source': body, name, locationOffsetts(2345)

What is the correct way to do it?

Thank you

ardatan commented 4 years ago

@francoisromain loadSchemaSync already returns GraphQLSchema so you don't need to call buildSchema. You can just do;

import { buildSchema } from 'graphql'
import { loadSchemaSync } from '@graphql-tools/load'
import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader'
import { join } from 'path'

const schema = loadSchemaSync(join(__dirname, 'schemas/index.graphql'), {
  loaders: [new GraphQLFileLoader()],
})

export default schema
francoisromain commented 4 years ago

@ardatan thank you for your answer. Typescript does not make an error anymore.

now, there is an other error:

/dist/import/src/index.js:235
                        throw new Error(`Couldn't find type ${dependencyName} in any of the schemas.`);
                              ^
Error: Couldn't find type Utilisateur in any of the schemas.

My schema is split in different files. With graphql-import, I was importing all the files into index.graphql with comments like so:

# import * from 'administrations.graphql'
# import * from 'utilisateurs.graphql'
# etc.

What is the new way to do it?

ardatan commented 4 years ago

Could you share a reproduction in a CodeSandbox project or a GitHub repo?

francoisromain commented 4 years ago

the error is caused by circular dependencies in graphql-tools.

here is the repro and the description of the bug: https://github.com/francoisromain/graphql-tools-config-debug

thank you

ardatan commented 4 years ago

@francoisromain It cannot find Enterprise here because it is not imported; https://github.com/francoisromain/graphql-tools-config-debug/blob/master/src/api/schemas/users.graphql#L5 So User cannot be found in here as well; https://github.com/francoisromain/graphql-tools-config-debug/blob/master/src/api/schemas/enterprises.graphql#L3

francoisromain commented 4 years ago

@ardatan thank you! I understand and don't have the import error anymore. but… I still have an error :sweat_smile:

This time I have 2 files: index.graphql and territoires.graphql.

index.graphql :

# import * from 'territoires.graphql'

type Query {
  pays: [Pays]
}

territoires.graphql

type Departement {
  id: ID!
}

type Region {
  id: ID!
  departements: [Departement]
}

type Pays {
  id: ID!
  regions: [Region]
}

The error is

/node_modules/graphql/validation/validate.js:108
throw new Error(errors.map(function (error) {
          ^
Error: Unknown type "Departement".

If I don't use a separate file and put everything in index.graphql like so, there is no error:

type Query {
  pays: [Pays]
}

type Departement {
  id: ID!
}

type Region {
  id: ID!
  departements: [Departement]
}

type Pays {
  id: ID!
  regions: [Region]
}

What am I doing wrong?

ardatan commented 4 years ago

@francoisromain Could you update your reproduction with your last scenerio? Thanks!

francoisromain commented 4 years ago

I updated the repo with this error: https://github.com/francoisromain/graphql-tools-config-debug :pray:

francoisromain commented 4 years ago

Hello @ardatan I see the issue is still marked as waiting for answer. Can I do something more about it?

ardatan commented 4 years ago

@francoisromain I removed the label. Working on it! Thanks for the reproduction :)

francoisromain commented 4 years ago

I re-opened the issue here: https://github.com/ardatan/graphql-tools/issues/1782