2

I have a database with the following structure.

Graph Database I'm writing a GraphQL resolver for the bottom-most node (the "rows" node).

As the image shows, each "rows" node corresponds to a specific path. (Company)->(DB)->(Table)->(rows)

A Query would be of the form:

{
  Company(name: "Google") {
    Database(name: "accounts") {
      Table(name: "users") {
        rows
      }
    }
  }
}

Question: How can I include/access Company.name, Database.name, Table.name information in the rows resolver so that I can determine which rows node to return?

In other words: I know I can access Table.name using parent.name, but is there a way to get parent.parent.name or parent.parent.parent.name?

If there isn't a way to access ancestor properties, should I use arguments or context to pass these properties manually into the rows resolver?

Note: I can't use the neo4j-graphql-js package.

Note: This is the first simple example I thought of and I understand there are structural problems with organizing data this way, but the question still stands.

2
  • Not sure if this is what you're after: graphql.org/graphql-js/passing-arguments another article with a clearer explanation: apollographql.com/docs/graphql-tools/resolvers (args: An object with the arguments passed into the field in the query. For example, if the field was called with author(name: "Ada"), the args object would be: { "name": "Ada" }. Commented Feb 6, 2020 at 15:27
  • @nopassport1 I know I could use arguments, but the problem with this is that I would be defining the query path twice: once as the normal GraphQL query (like the one posted in my question), and again as an argument for rows. Thank you either way, I'll make my question more specific. Commented Feb 6, 2020 at 15:49

1 Answer 1

5

You can extract the path from the GraphQLResolveInfo object passed to the resolver:

const { responsePathAsArray } = require('graphql')

function resolver (parent, args, context, info) {
  responsePathAsArray(info.path)
}

This returns an array like ['google', 'accounts', 0, 'user']. However, you can also pass arbitrary data from parent resolver to child resolver.

function accountResolver (parent, args, context, info) {
  // Assuming we already have some value at parent.account and want to return that
  return {
    ...parent.account,
    message: 'It\'s a secret!',
  }
}

function userResolver (parent, args, context, info) {
  console.log(parent.message) // prints "It's a secret!"
}

Unless message matches some field name, it won't ever actually appear in your response.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.