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

Schema File #31

Closed
alexsteinerde opened this issue Jan 16, 2018 · 9 comments
Closed

Schema File #31

alexsteinerde opened this issue Jan 16, 2018 · 9 comments

Comments

@alexsteinerde
Copy link
Member

It would be great to have the ability to provide a schema file to the server and start it with the file content.
Or is this already possible just not listed in the readme?
Thanks
Alex

@paulofaria
Copy link
Member

You mean generate a .switft file from a graphql schema?

@alexsteinerde
Copy link
Member Author

No I mean a .graphql file where I can specify the schema like in code.

@paulofaria
Copy link
Member

how are you going to define the resolvers with a .graphql file?

@roschlau
Copy link

I don't know if this can be done in Swift in the same way, but with the Java implementation of GraphQL, you can just provide the schema file, and the library will automatically map the fields in the schema to the corresponding fields in the data model, or to appropriately named resolver functions if no fields are found: https://github.com/graphql-java/graphql-java-tools#resolvers-and-data-classes

@paulofaria
Copy link
Member

@Hikaru755 This would be possible in Java by creating classes out of thin air? If so, Swift doesn't allow this. It's reflection/introspection/dynamic capabilities are very limited so far.

@roschlau
Copy link

roschlau commented Jan 19, 2018

It doesn't create classes on the fly, you still have to define all classes and resolvers for the types in the schema yourself (or generate them via other means). It's just the mapping between them and the schema fields that happens automatically. Basically, if you can get a list of methods of an object and call them dynamically by their name it should be possible. Let me explain by example, if we have the following GraphQL schema file:

type Query {
    user: User
}
type User {
    name: String
}

In Java, when you first configure your GraphQL endpoint, you have to pass that schema plus a QueryResolver to the library, as well as any additional resolvers you need. If a request comes in, like this: query { user { name } } the library will look for a method on the query resolver that maps to the user-field (simply by being called user() or getUser()). It calls that method, takes its return value (for example an instance of a class called User), and checks if it has a method called name() or getName() on it. If it does, it calls that method and outputs its return value. If it doesn't, it then looks for any resolvers that can take a User instance and that have a method called name or getName, and calls those instead.

It just lets you define your schema in a language-agnostic way, and uses it to choose the correct resolver functions by naming convention, instead of having to verbosely build the schema in your implemenation logic and attach the resolvers to the fields manually.

@paulofaria
Copy link
Member

Got it! Thanks for the thorough explanation. I believe it's possible to implement that feature in the way you mentioned. Currently I don't have as much time as I want to implement this. If anyone is interested I can give directions.

@paulofaria
Copy link
Member

Also, I believe such a high level feature belongs to a higher level framework. Maybe github.com/graphqlswift/graphiti or another standalone framework.

@alexsteinerde
Copy link
Member Author

That sounds logic to me and I have not found Grafphiti before. So thanks for the link. I will create an issue there and close it here.
Again thank you for the help.

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