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

How to handle schema declarations in separate files? #49

Closed
christopher-caldwell opened this issue Mar 23, 2023 · 10 comments
Closed

How to handle schema declarations in separate files? #49

christopher-caldwell opened this issue Mar 23, 2023 · 10 comments

Comments

@christopher-caldwell
Copy link

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?

@mishushakov
Copy link
Member

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

@christopher-caldwell
Copy link
Author

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.

@mishushakov
Copy link
Member

Thanks for elaborating 🙏
I will change the buildSchema method to take an array of types instead of GarphSchema

@christopher-caldwell
Copy link
Author

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!

@christopher-caldwell
Copy link
Author

Any update here?

@mishushakov
Copy link
Member

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

@christopher-caldwell
Copy link
Author

Anything new here?

@ethras
Copy link

ethras commented Aug 22, 2023

Wondering the same ?

@mishushakov
Copy link
Member

Would you like to sponsor this feature?

mishushakov added a commit that referenced this issue Sep 23, 2023
@mishushakov
Copy link
Member

mishushakov commented Sep 23, 2023

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 😌)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants