5

I'd like to search, filter, order over an in memory result which is aggregated from data of a local database and data from an external api.

This is my example resolver.

const resolver = {
    Query: {
        allDevices: (_, args: QueryArgs, context: CustomContext) =>
            readAllDevices() // from local database
                .then(searchFilterOrderAndPaginate(["name"], args)) 
                // additionalDataFromOtherMicroservice is not yet resolved
                // -> no search possible over all properties
    }, 
    Mutation: {},
    Device: {
        additionalDataFromOtherMicroservice: () => Promise.resolve("" + Math.random())
        // simulates an http request to another microservice api
    }

};

This is my example schema.

const schema = gql`

    type Device {
        id: ID
        number: ID
        name: String
        additionalDataFromOtherMicroservice: String
    }

    type Query {
        allDevices(
            search: String
            filterParams: [FilterParam]
            orderParams: [OrderParam]
            pagination: Pagination
        ): [Device]
`

The problem is that grapqhl resolves the fields as a cascade. I don't have access to the root query when a field resolver has been finished.

I tried https://www.npmjs.com/package/graphql-middleware but doesn't fires after all subfields are resolved.

How is it possible to use graphql's powerful field resolving mechanism incl. caching etc AND access the result AFTER all fields have been resolved?

riemerio
  • 101
  • 7

0 Answers0