[swydo:graphql] Get error, If I have 2 .graphql schemas that import each other

HI all, I use swydo:graphql.
I get error when If I have 2 .graphql schemas that import each other (https://github.com/Swydo/meteor-graphql/issues/27)

// Comment.graphql
#import './User'

type Comment {
  title: String!
  isDone: Boolean!
  createdAt: Date!
  user: User
}

// User.graphql
#import './Comment'

type User {
  _id: ID!
  firstName: String!
  lastName: String!
  age: Int
  comments: [Comment]
}
Fails with:

W20180310-07:05:55.673(2)? (STDERR) TypeError: Cannot read property 'filter' of undefined
W20180310-07:05:55.673(2)? (STDERR)     at unique (/Users/theodor/Projects/graphql-live/.meteor/local/build/programs/server/app/app.js:30:19)
W20180310-07:05:55.673(2)? (STDERR)     at Comment.graphql.js (/Users/theodor/Projects/graphql-live/.meteor/local/build/programs/server/app/app.js:43:44)
W20180310-07:05:55.673(2)? (STDERR)     at fileEvaluate (packages/modules-runtime.js:343:9)
W20180310-07:05:55.674(2)? (STDERR)     at require (packages/modules-runtime.js:238:16)

Could help me?

You don’t need those imports. Imports are required for fragment resolving.

Can you try simply removing those import statements? Are you receiving any errors then?

You only need to load all graphql files, and make sure to register all the types. Your Comment.graphql can use “undefined” types in that scope. It will be fixed upon register.

Thanks for your reply.
Yes it still work fine, if I don’t use #import .....
But I would like to split file (type, schema, resolvers) like this:

  • Comments
// comment.graphql
#import User "../uers/user.graphql"

type Comment {
  title: String!
  isDone: Boolean!
  createdAt: Date!
  user: User
}
type OtherCommentType {
  ..............
}
----------
// schema.js
#import Comment "./comment.grapql"

type Query {
  getComments: [Comment]
}
---------
// resolvers.js
.........
  • Users
// user.graphql
#import Comment "../comments/comment.grapql"

type User {
  _id: ID!
  firstName: String!
  lastName: String!
  age: Int
  comments: [Comment]
}
type OtherUserType {
  ..............
}
----------
// schema.js
#import User "./user.grapql"

type Query {
  getUsers: [User]
}
---------
// resolvers.js
.........
  • Startup api
import { mergeTypes, mergeResolvers } from 'merge-graphql-schemas'

// Get schemas
import { typeDefs as userType, resolvers as userResolver} from './users/schema.js'
import { typeDefs as commentType, resolvers as commentResolver} from './comments/schema.js'

// Create apollo server
const server = new ApolloServer({
  typeDefs: mergeTypes([userType, commentType], { all: true }),
  resolvers: mergeResolvers([userResolver, commentResolver]),
.............

i think you just need to import one into the other. Or maybe cleaner: import all in one index file.

It’s not like your usual import statements, where you pull stuff into scope, its more like concatination.

Exactly what @macrozone writes. It’s more like a concatenation.

If you really want to import inside the graphql files, which I wouldn’t do myself, just stick to imports down the tree, not up the tree.

Using a Comment type inside a User type, doesn’t mean that you need to import it there.

thanks for all :sunny: