Apologies if this request doesn’t necessarily make sense in terms of what I should be using apollo & GQL for.
So. We have various points in our schema where there are various fields of the syntax thingId
- which in turn relate to a related type, defined elsewhere in the Schema. For example, in our User
objects we have:
type User {
id
projectId
}
Here, the User
type has an associated projectId
that maps to a type Project
defined elsewhere in the schema.
What I’ve been hoping to achieve with apollo, is the ability to define a custom field called project
, an when it exists a discreet async Query (projectById(...)
) is made, returning a project for that given ID. This custom field would be given within the userById()
query template, invoked from the front-end app (powered by apollo-client
). So in the same User
example, the front-end’s template would look like:
query userById($id: ID!) {
userById(id: $id) {
id
project {
id
name
# etc. etc.
}
}
}
I had thought perhaps resolvers were the path here, knowing they could be asynchronous and I could use their context’s dataSources
to make discreet requests (and added this resolver using graphql-tools
toolset) … but of course, because project
isn’t part of the schema, an error is thrown. User.project defined in resolvers, but not in schema
First: am I trying to “fit a round peg in a square hole” and misusing apollo, or is there a (sane!) path towards achieving this kind of custom field definition?
Am hoping the community can help: I know that this can be achieved if I build my schema using something like type-graphql
- but I really don’t want to add to the overhead of the apollo-server
codebase, increase maintenance complexity - and introduce performance bottlenecks. So instead hoping for a simple solution for adding custom fields to Queries.