GraphQL Overview
Beta release
This is pre-release documentation for an API in public beta and is subject to change.
GraphQL streamlines your application flow by letting you request exactly the data you need. GraphQL can reduce development time and improve the customer experience by making requests more efficient. With fewer, faster, and more compact data transfers, GraphQL can be especially beneficial for resource-constrained mobile applications and in modern frontend development.
The open-source GraphQL standard defines both a query language and a runtime that retrieves data using queries. A single GraphQL query can retrieve data that would require multiple Square API calls.
Square GraphQL features include:
A standards compliant GraphQL endpoint that serves multiple Square graphs. You can send one query to retrieve exactly the data you need from multiple Square objects and graphs.
Support for OAuth access management.
Support for testing in the Square Sandbox environment.
Statically typed schemas that enable developer tooling. You can view the schema documentation when you sign in to GraphQL Explorer.
Square GraphQL supports query operations only; it doesn't support mutations or subscriptions. Applications that want to use GraphQL for faster querying and need to perform write operations must use both GraphQL and Square APIs or Square SDKs.
Square GraphQL supports query operations for the following Square graphs:
Cards
Catalog
Customers
Inventory
Merchants
Orders
Payments
Refunds
A graph provides similar read access to data as the corresponding Square API, as well as access to data that crosses API boundaries. For example, an Orders query can return the email address, phone number, and other attributes of the customer associated with the order.
Square provides two endpoints where you can send GraphQL queries:
For queries in the production environment, call:
https://connect.squareup.com/public/graphql
For queries in the Sandbox test environment, call:
https://connect.squareupsandbox.com/public/graphql
Unlike Square API requests, a GraphQL query specifies the exact data to return, including data that crosses API boundaries. To get started making GraphQL queries against Square data, try GraphQL Explorer. You can choose from several sample queries or write your own queries. For more information, see GraphQL basics or GraphQL Explorer Overview.
Did you know?
To get the required merchant ID for your Square GraphQL queries, query currentMerchant
for the id
field. (See an example.)
The Square GraphQL endpoint also works with third-party GraphQL tools, such as GraphiQL.
For example, this example cURL query retrieves an order and the customer who made the order:
The data
object in the response contains the query results. If any errors are encountered while processing the query, Square returns an errors
list. A response can contain both data
with (partial) query results and errors
.
Tag your feedback, issues, and questions with graphql-api
and post them on the Square Developer forums or email [email protected].
You should understand the following concepts when working with Square GraphQL queries:
Objects - An object is a collection of fields. The data type of a field can be a scalar, complex type, or enum, as a single value or an array. For example, the
Customer.phoneNumber
field is aString
type and theCustomer.address
field is anAddress
type.Enums - An enum defines the set of valid values for fields of the given enum type. For example, the
Currency
enum defines all possible values for theMoney.currency
field.Interfaces - An interface defines fields that must be included in any object that implements the interface. For example,
CatalogItem
andCatalogCategory
implement theCatalogObject
interface, so both objects include allCatalogObject
fields. The Catalog sample query in GraphQL Explorer shows how you can use inline fragments to request fields based on different implementations.Non-Null - An exclamation mark (
!
) appended to the data type of a field indicates that the value of the field must be non-null when returned or when provided as an argument.Schema - A schema defines the data model (graph). It describes the objects and fields that are available to query, as well as the entry points, hierarchical relationships, arguments, and type information that determine valid query syntax. You can view the Square GraphQL schema in the Documentation Explorer pane in GraphQL Explorer.
For more information about GraphQL basics, see Introduction to GraphQL at graphql.org.
A query is an operation that retrieves specific data. Every query contains at least one graph entry point (such as orders
, catalog
, or customers
) and specifies the fields to return. Because queries are also objects, you can name and reuse queries.
A query is enclosed in curly braces ({ }
). Lines that begin with a #
character are treated as comments and ignored. For example, a simple query might use the following format:
The following example query from GraphQL Explorer requests the status
and mainLocation
fields for a given merchant:
Because mainLocation
is a complex type, you must also specify the Location
subfields that you want to retrieve. If successful, the query returns results similar to the following:
You can optionally specify the query
operation type and operation name in your queries. Naming a query can help with reuse, debugging, and logging.
You can filter a query to conditionally retrieve data based on specified criteria. Many Square GraphQL filters are equivalent to filters used for list and search endpoints in Square APIs.
All graph entry points (except currentMerchant
) accept a filter
argument and define valid filtering criteria in a top-level input type. For example, the following cardsOnFile
query uses fields from the CardOnFileFilter
input type to retrieve the cards on file for a customer that are enabled for payments:
Most graph entry points allow you to specify an id
filter that lets you retrieve a specific object. The following example query uses the id
filter to retrieve a specific payment:
All graph entry points (except currentMerchant
) require a merchant ID filter. You can use currentMerchant
to get the merchant ID and other merchant-related fields, as shown in the following example query:
You can use variables to replace static or dynamic values in query arguments. For example, by using a variable for the merchant ID that's required to access Square data, you can easily reuse the query for different merchants.
The format for a variable is $variableName
(a dollar sign followed by the variable name). To use a variable in a query, you must:
Declare that the variable is used in the query.
Replace the value in the query with the variable.
Define the value of the variable.
The following example cURL query uses two variables, $merchantId
and $searchText
:
Note how the variables are used in the example query:
The variables and their types are declared as arguments for the query operation:
query CatalogTextSearch($merchantId: ID!, $searchText: [String!]!)
The
$merchantId
variable is used by themerchantId
filter in the query:
merchantId: { equalToAnyOf: [$merchantId]}
The
$searchText
variable is used by thetext
filter in the query:
text: { value: $searchText}
The values for both variables are defined in the
variables
object:
"variables": { "merchantId": "8QJT7EXAMPLE", "searchText": ["gluten", "free"]}
Values are passed in a separate, transport-specific (usually JSON) variables dictionary. You can also use a separate JSON file to pass in variable values.
In GraphQL Explorer, you can declare and reference variables in query arguments using the $variableName
format, similar to the preceding example. To define the values of variables, use the Query Variables pane.
The sample queries available in GraphQL Explorer show how to use variables in queries and pass in their values.
Pagination is used to retrieve all the results of a query using paged responses. Square GraphQL uses the cursor method of pagination. You retrieve pages of data by optionally specifying the page size and then use a pointer (cursor) to keep track of where the next page of data should be retrieved from.
The following example query uses pagination to retrieve all orders for a given merchant and location:
The
first
field specifies the page size, which is the maximum number of items to return in each response page. If you omit this field, the default page size is used.The
pageInfo
field requests thehasNextPage
andendCursor
to use for forward pagination.
The following is an example paged response:
If more pages of data are available, hasNextPage
is true
and endCursor
contains the cursor.
In your next query, include the after
field and provide the cursor from the previous response, as shown in the following example:
Continue using the cursor from each response page in a subsequent query until you retrieve all response pages.
In the last response page, hasNextPage
is false
and endCursor
is null
, as shown in the following example:
The pagination process might vary depending on the Square graph being queried. For example, payments and refunds can also be paginated in a backward direction using the last
and before
fields and the startCursor
cursor.
In addition, valid first
and last
page size values might vary depending on the graph or the complexity score of the query.
GraphQL queries must provide a bearer access token in the Authorization
header, just like Square API requests. The access token determines the data that you have permissions to view.
A personal access token grants full access to the data in your Square account. If you query using a personal access token, make sure to use one that's valid for the target Sandbox or production environment.
An OAuth access token grants granular access based on a set of permissions. If you query using an OAuth access token, the results are scoped to the permissions you requested and were granted. For example, if an
orders
query includes fields for the customer associated with the order, you need bothORDERS_READ
andCUSTOMERS_READ
to get full results. For more information about the OAuth flow, see OAuth API Overview.Fields that you're not permitted to view are set to
null
in the results. In addition, the response contains anerrors
list with one or moreSCOPE_MISSING
errors. The error message provides related information, such as the missing OAuth scope.
{% accordion expanded=false %} {% slot "heading" %}
{% /slot %}
If you're using GraphQL Explorer, the access token for the query is provided in the Request Headers pane. When signed in with your Square account, you can use the Add access token button to select a personal access token that grants full access to all data in your Square account. To test a specific set of permissions, you can paste an OAuth access token into the Request Headers pane. {% /accordion %}
Rate limits are intended to enforce reasonable API usage. Square uses a calculated query cost method for rate limiting based on the following limits:
Applications can send a maximum of 10 queries per second (10 QPS).
A query can have a maximum depth of 10. Query depth is the number of nested levels of the query fields.
A query can have a maximum complexity score of 250 points.
The 250 maximum complexity score combined with the 10 QPS limit results in an implicit limit of 2500 complexity points consumed per second.
Queries that exceed the maximum complexity score return a
COMPLEXITY_MAX_REACHED
error.
For testing or debugging purposes, you can direct Square to return the complexity score for a query. To do so, include the x-graphql-include-debug
request header and set the value to 1. In GraphQL Explorer, add the header to the Request Headers pane.
Square returns the complexity score in the extensions
object, as shown in the following excerpt:
{% accordion expanded=false %} {% slot "heading" %}
{% /slot %}
You can calculate the cost of a query using the following point scale.
Two points for each graph entry point, connection object, and join relationship - A query contains at least one graph entry point and one connection object
A one-to-many relationship that enables access to items as a paginated list.orders
entry point, two points for thenodes
connection object, and two points for therefunds
join relationship.The
location
object is counted next.One point for each object, union, or interface - In a query, every specified field whose data type is an object, union, or interface costs one point. For the previous example query, add one point for the
location
object, which brings the total complexity score of the query to seven points.Note that when a query includes a
first
orlast
page size argument, the specified value also counts as points.Zero points for scalars and enums - Scalars and enums don't incur a cost because they return a final value in query results. In the example query,
reason
is aString
scalar value andid
is anID
scalar value. {% /accordion %}
{% accordion expanded=false %} {% slot "heading" %}
{% /slot %}
In a query, the first
and last
arguments can be used to specify the page size, which is the maximum number of results to include in a single page of the response. For queries that include a first
or last
argument, the specified page size value counts as points. For example, first: 20
adds 20 points to the complexity score.
The complexity score might also determine the maximum possible page size for a query. As the complexity score increases, the maximum possible page size decreases. For example, the most simple orders
query costs four connection points, which effectively limits the maximum page size to 246. Adding location
to the query reduces the limit to 245.
Some graphs enforce a lower maximum page size limit. For example, a customers
query has a maximum page size of 20. In this case, the complexity score is less likely to result in rate limiting based on the query complexity.
{% /accordion %}
You should be aware of the following limitations when working with Square GraphQL.
Square GraphQL supports query operations only; it doesn't support mutation or subscription operations. Applications that need to perform both read and write operations should use Square APIs or Square SDKs.
Rate and query cost limits are applied to Square GraphQL queries.
Square SDKs cannot be used to send GraphQL queries.
The API Logs feature in the Developer Dashboard does not include GraphQL queries.
Not all Square APIs have corresponding GraphQL support. For the list of supported graphs, see Supported Square graphs.
Square graphs generally provide access to the same data as the corresponding Square APIs, as well as expanded, direct access to referenced objects that cross API boundaries. However, graphs are not always at parity with corresponding Square APIs. For example:
Compared to the corresponding list or search endpoints, GraphQL provides more filters for payments but fewer filters for customers.
You cannot retrieve custom attributes for merchants, orders, or customers using GraphQL queries. Only catalog custom attributes are supported.
Although Square graphs are continually updated to reflect the latest version of the corresponding Square APIs, there might be exceptions. You should use the current schema as the source of truth for GraphQL support. You can view the schema in GraphQL Explorer or by using the introspection system, as shown in this simple introspection query:
For more information, see Introspection at graphql.org.
The Square GraphQL schema generally reflects the latest version of the corresponding Square APIs. The following are backward-incompatible changes to the schema that might require updates to your application code.
Effective date: 2023-05-23
The following CatalogItem
field is changed:
Previous name and type | New name and type |
---|---|
image: CatalogImage | images: [CatalogImage] |
Effective date: 2023-04-12
The following OrderFilter
fields are renamed:
Previous name | New name |
---|---|
customers | customer |
ids | id |
locations | location |
merchants | merchantId |
sourceNames | sourceName |
states | state |
Effective date: 2023-04-12
The required merchant ID is now passed using the id
filter.
Example: Previous argument
Example: Current argument
Effective date: 2023-04-12
The following CustomerFilter
field is renamed:
Previous name | New name |
---|---|
ids | id |
Effective date: 2023-04-01
The Catalog graph now provides separate entry points for querying catalog objects and catalog items. As part of this change, the all
and items
top-level fields are removed. In addition, the merchant ID is now passed as a filter.
Example: Previous catalog objects query
Example: Current catalog objects query
Example: Previous catalog items query (uses the catalog
entry point)
Example: Current catalog items query (uses the catalogItems
entry point)
Effective date: 2023-03-28
The following CatalogFilter
fields are renamed:
Previous name | New name |
---|---|
ids | id |
itemVariationsForItemOptionValues | itemVariationsForItemOptionValue |
itemsForItemOptions | itemsForItemOption |
categoryIds | categoryId |
customAttributes | customAttribute |
enabledLocationIds | enabledLocationId |
productTypes | productType |
If you need more assistance, contact Developer Support or ask for help in the Developer Forums.