stepci/garph

How to handle schema declarations in separate files?

christopher-caldwell opened this issue ยท 10 comments

All of the examples have all the schemas in the same file, then build the schema at the end for the bottom of the file.

Is it possible to put these declarations in separate files?

I tried to use convertSchema, but to put them all as references into the types array and got the error:

Error: Type with name "undefined" does not exists

Then I tried to import g, and export g so I could use the same reference to g in separate files and got the error:

Error: Type with name "Query" does not exists

After that, I moved all of them into the same file, and it works fine. Is it possible to keep the schemas in separate files?

Maybe try this:

import { GarphSchema } from 'garph'

export const g = new GarphSchema()

And then you could reuse the g in your other files as well
Let me know if this works

No, this didn't work. Logging g.types is an empty array.

I did manage to get this working by using a "factory" pattern.

import { buildSchema, g } from 'garph'

import { resolvers } from '@_api/resolvers'
import { buildFamilyMemberSchema } from './familyMember'
import { buildMutationsSchema } from './mutations'
import { buildPhotosSchema, buildUploadResponseSchema } from './photos'
import { buildQueriesSchema } from './queries'
import { buildDate, buildMutationResultSchema } from './shared'

const mutationResultSchema = buildMutationResultSchema(g)
const dateSchema = buildDate(g)
const uploadResponseSchema = buildUploadResponseSchema(g)
const familyMemberSchema = buildFamilyMemberSchema(g)
const photoSchema = buildPhotosSchema(g, familyMemberSchema)
buildMutationsSchema(g, mutationResultSchema, uploadResponseSchema, dateSchema)
buildQueriesSchema(g, familyMemberSchema, photoSchema)

export const schema = buildSchema({ g, resolvers })
console.log('schema', schema)

This works, but is rather inconvenient.

Thanks for elaborating ๐Ÿ™
I will change the buildSchema method to take an array of types instead of GarphSchema

Yeah, sure thing! Thanks for making this. My default is codegen, but I definitely prefer this.

Maybe even just an alternative, like buildSchemaFromTypes or something. That way if people want to declare everything in one file, they can. Nice to have options!

Any update here?

Glad you asked!

This week we had to work on the website and some internal stuff. So, I will be looking into it next week

Anything new here?

ethras commented

Wondering the same ?

Would you like to sponsor this feature?

You can now create a Garph schema with initializer

import { GarphSchema } from 'garph'

const g = new GarphSchema({ types: [a, b, c] })

Or

import { GarphSchema } from 'garph'

const g = new GarphSchema()
g.types = [a, b, c]

(sorry it took us so long ๐Ÿ˜Œ)