Setting Up GraphQL Clients
To maximize efficiency and code reusability when integrating with the Causely GraphQL API, we recommend setting up proper GraphQL client libraries rather than using raw HTTP requests. This approach provides better error handling, type safety, query validation, and cleaner code that can be reused across multiple API operations.
Install GraphQL Client Libraries​
Install the recommended GraphQL client libraries for your programming language to enable robust API integration with automatic query validation and enhanced developer experience:
- python
- javascript
- bash
- go
pip install gql[all] requests
npm install @apollo/client graphql
# Install gq (GraphQL CLI tool)
curl -sSL https://github.com/houseabsolute/gq/releases/latest/download/gq-Linux-x86_64.tar.gz | tar xz
# Or use your package manager, e.g., brew install gq
go get github.com/machinebox/graphql
Create Reusable GraphQL Client Functions​
Implement these language-specific GraphQL client functions that integrate seamlessly with the authentication methods from the previous section. These functions provide a clean, reusable interface for executing any GraphQL query against the Causely API:
- python
- javascript
- bash
- go
from gql import gql, Client
from gql.transport.requests import RequestsHTTPTransport
def create_graphql_client(access_token):
"""Create a GraphQL client with authentication."""
transport = RequestsHTTPTransport(
url="https://api.causely.app/query/",
headers={"Authorization": f"Bearer {access_token}"}
)
return Client(transport=transport, fetch_schema_from_transport=True)
def post_query(client, query_string, variables=None):
"""Execute a GraphQL query using the client."""
query = gql(query_string)
return client.execute(query, variable_values=variables)
# Usage example:
# client = create_graphql_client(get_causely_access_token(client_id, secret))
# result = post_query(client, query_string, variables)
const { ApolloClient, InMemoryCache, createHttpLink, gql } = require('@apollo/client');
function createGraphQLClient(accessToken) {
const httpLink = createHttpLink({
uri: 'https://api.causely.app/query/',
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
return new ApolloClient({
link: httpLink,
cache: new InMemoryCache(),
});
}
async function postQuery(client, queryString, variables = {}) {
const result = await client.query({
query: gql(queryString),
variables,
fetchPolicy: 'no-cache',
});
return result.data;
}
// Usage example:
// const client = createGraphQLClient(await getCauselyAccessToken(clientId, secret));
// const result = await postQuery(client, queryString, variables);
# Function to execute GraphQL queries using gq
post_query() {
local query_file="$1"
local variables_file="$2"
gq https://api.causely.app/query/ \
--header "Authorization: Bearer ${CAUSELY_ACCESS_TOKEN}" \
--query-file "$query_file" \
--variables-file "$variables_file"
}
# Alternative using curl with GraphQL
post_query_curl() {
local query="$1"
local variables="$2"
curl -X POST https://api.causely.app/query/ \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${CAUSELY_ACCESS_TOKEN}" \
-d "{\"query\": \"$query\", \"variables\": $variables}"
}
package main
import (
"context"
"github.com/machinebox/graphql"
)
func createGraphQLClient(accessToken string) *graphql.Client {
client := graphql.NewClient("https://api.causely.app/query/")
client.WithHTTPHeader("Authorization", "Bearer "+accessToken)
return client
}
func postQuery(client *graphql.Client, queryString string, variables map[string]interface{}) (interface{}, error) {
req := graphql.NewRequest(queryString)
// Add variables to the request
for key, value := range variables {
req.Var(key, value)
}
var response interface{}
err := client.Run(context.Background(), req, &response)
return response, err
}
// Usage example:
// client := createGraphQLClient(token)
// result, err := postQuery(client, queryString, variables)