Development Guides
GraphQL Query

GraphQL Query

Introduction

When developing an application with the ROQ platform, you can use API from the ROQ SDKs, such as ROQ NOde.js SDK, or use directly with GraphQL query.

Suppose you want to develop the application by using GraphQL. In that case, ROQ provides the graphqlRequest() method to query or mutate data directly.

ℹ️

Before you use these APIs, please install the ROQ's Node.js SDK

graphqlRequest()

The benefit of using graphqlRequest(), you don't need any additional package dependencies installed, and everything is already setup internally, such as authentication and the GraphQL server URL. All you have to do is provide the GraphQL query.

This API method has the signature:

graphqlRequest(query, variables, requestHeaders);

For example, if you want to query data for specific users by using the key user id. You can use a static GraphQL query with a known user ID:

query {
  users(
    filter: { reference: { 
      equalTo: "8d04d09a-8ff8-4393-840d-c69f3f1f7dfa"}}
  ) {
    totalCount
    data {
      id
      tenantId
    }
  }
}

And then directly call the GraphQL query with graphqlRequest() method.

import { Platform } from '@roq/nodejs'
 
const roqClient = new Platform({
	apiKey: process.env.ROQ_API_KEY,
	environmentId: process.env.ROQ_ENVIRONMENT_ID,
	host: process.env.ROQ_PLATFORM_URL
})
 
const graphqlQuery = `
  query {
  users(
    filter: { reference: { 
      equalTo: "8d04d09a-8ff8-4393-840d-c69f3f1f7dfa"}}
  ) {
    totalCount
    data {
      id
      tenantId
    }
  }
}
`
const queryResult = await roqClient.asSuperAdmin().graphqlRequest(graphqlQuery)

or for dynamic variables you can use variables option in the graphqlRequest() method:

const queryUsers = `
query users( $filter: UserFilterArgType ) {
	users( filter: $filter ) {
	  totalCount
	  data {
		id
		email
		tenantId
	  }
	}
}
`
const userId = "f4c09fef-843f-4501-acd3-0847d3b417c0"
const graphqlRequestVars = {
	filter: {
		id: {
			equalTo: userId
		}
	}
}
 
const queryUsersByGraphQL = await roqClient.asSuperAdmin().graphqlRequest(queryUsers, graphqlRequestVars);

You can access this API as a Super Admin or a regular User. If you use GraphQL query as a regular user, you have to know the user ID. You can look into this Users API to get any user ID.

const userID = "805420cb-e75c-4e6d-8365-064babe18ec1"
const queryData = await roqClient.asUser(userID).graphqlRequest(queryUsers, graphqlRequestVars)

For more information about Super Admin, please refer to the user vs superadmin section of the SDK API documentation.