Can we pass dynamic schema details in GraphQL query which can be used by JoinMonster?

Viewed 54

I'm not sure if passing the schema name as an argument in the GraphQL query will work for JoinMonster or not. Following is the query example:

{
  configuration_queries(table: 'xyz', where: 'some condition') {
     data {
        col1
        col2
     }
  }
}

so here it is expected to create a dynamic query on "xyz" table using JoinMonster.

1 Answers

Yes, it's possible to pass schema details dynamically to GraphQL queries. There are a few different ways to do this, but one approach would be to use the graphql-tools package:

  1. Install the graphql-tools package:
npm install --save graphql-tools
  1. Write your GraphQL query, using the graphql-tools syntax for dynamic schema details:
const query = `
  query($schema: String!) {
    joinMonster(sql: $schema)
  }
`;
  1. Call the graphql function, passing in your query and dynamic schema details:
const schemaDetails = `
  SELECT * FROM users
  JOIN posts ON users.id = posts.user_id
`;

graphql(query, { schema: schemaDetails })
  .then((result) => {
    // handle results...
  })
  .catch((error) => {
    // handle errors...
  });
Related