apollographql / apollo

:rocket: Open source tools for GraphQL. Central repo for discussion.
https://www.apollographql.com
MIT License
2.62k stars 425 forks source link

Can I do partial type definition? #74

Closed ron-liu closed 7 years ago

ron-liu commented 7 years ago

I know apollo-tools support merge multiple graphql files, but can I go further to split one type across multiple files, especially for the root schema like below?

# author.js
export default [`
type Author {
   id: ID!
   name: String!
}
type Query {
  authors: [Author]
}
`]
# book.js
export default [`
type Book {
   id: ID!
   name: String!
}

type Query {
  books: [Book]
}
`]
// schema.js
import authorSchema from './author'
import bookSchema from './book'
const rootSchema = [`
schema {
  query: Query
}
`]
export const schema = [...bookSchema, ...authSchema, ...rootSchema] 
/*
I am expecting to have 
type Query {
    books: [Book]
    authors: [Author]
}
*/
stubailo commented 7 years ago

I think that would be really really useful, and we want to implement that as well. Do you have any ideas for how this can be achieved? I'd say the best place to discuss is on graphql-tools where this functionality is implemented: https://github.com/apollostack/graphql-tools

ron-liu commented 7 years ago

@stubailo

Sorry I posted in wrong place, could you move this ticket this time?

I guess the root cause is graphl spec doesn't support it. I am going to walk around like below:

# author.js
export const gql = [`
type Author {
   id: ID!
   name: String!
}
`]
export const rootQuery = [`
  authors: [Author]
`]
# book.js
export const gql = [`
type Book {
   id: ID!
   name: String!
}
`]
export const rootQuery = [`
  books: [Book]
`]
// schema.js
import {gql:authorSchema, rootQuery: authroRootQuery} from './author'
import {gql: bookSchema, rootQuery: bookRootQuery} from './book'
export const schema = [...bookSchema, ...authSchema, `
  type Query{
    ${[...authRootQuery, ...bookRootQuery].join('\n')}
  }
`] 

I know it is not a good idea :(, but that's all I can think of.

stubailo commented 7 years ago

There's no way to move tickets, sorry!

helfer commented 7 years ago

@ron-liu do you know about the extend syntax in GraphQL? It's not perfect, but maybe it will help you implement what you need? graphql-tools already supports it.