Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regarding circular dependencies #126

Closed
m64253 opened this issue Sep 8, 2016 · 4 comments
Closed

Regarding circular dependencies #126

m64253 opened this issue Sep 8, 2016 · 4 comments

Comments

@m64253
Copy link

m64253 commented Sep 8, 2016

This how I understood how to handle circular dependencies, wrap in function & return array. Reference: http:https://dev.apollodata.com/tools/graphql-tools/generate-schema.html

So if I do this:

// file: Author.js
const Book = require('./Book');

const Author = `
  type Author {
    name: String
    books: [Book]
  }
`;

module.exports = () => [Author, Book];

// -----------------------------------

// file: Book.js
const Author = require('./Author');

const Book = `
  type Book {
    title: String
    author: Author
  }
`;

module.exports = () => [Book, Author];

// -----------------------------------

// file: schema.js
const Author = require('./Author');

const RootQuery = `
  type RootQuery {
    author(name: String): Author
  }
`;
const SchemaDefinition = `
  schema {
    query: RootQuery
  }
`;

const schema = makeExecutableSchema({
  typeDefs: [SchemaDefinition, RootQuery, Author],
  resolvers: {}
});

It will result in the following error:

Error: typeDef array must contain only strings and functions, got object

I'm sure I misunderstood something and any help pointing me in the right direction is highly appreciated.

@linonetwo
Copy link

There is no need to import Book to where Author is, just combine them at schema.js. Since the order of graphQL file is unaffected with it.

@helfer
Copy link
Contributor

helfer commented Sep 9, 2016

I think the error you're getting because require('./Author') actually gives you an object, which contains more stuff than what you exported. Other than that, it all looks correct.

@m64253
Copy link
Author

m64253 commented Sep 9, 2016

Thanks for the replies it turns out that all it need was some rubber ducking to realise the issue. All I needed to do is handle circular dependencies node style, export before require.

Like this:

// file: Author.js
module.exports = () => [Author, Book];

const Book = require('./Book');

const Author = `
  type Author {
    name: String
    books: [Book]
  }
`;
// -----------------------------------

// file: Book.js
module.exports = () => [Book, Author];

const Author = require('./Author');

const Book = `
  type Book {
    title: String
    author: Author
  }
`;
// -----------------------------------

// file: schema.js
const Author = require('./Author');

const RootQuery = `
  type RootQuery {
    author(name: String): Author
  }
`;
const SchemaDefinition = `
  schema {
    query: RootQuery
  }
`;

const schema = makeExecutableSchema({
  typeDefs: [SchemaDefinition, RootQuery, Author],
  resolvers: {}
});

@m64253 m64253 closed this as completed Sep 9, 2016
@eddyerburgh
Copy link

If anyone wants to see this in a working project - https://github.com/eddyerburgh/express-graphql-boilerplate

trevor-scheer added a commit that referenced this issue May 6, 2020
trevor-scheer added a commit that referenced this issue May 14, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants