GraphQL

Short description: Data query language developed by Facebook

GraphQLFile:Frameless

Original author(s)| Facebook
Developer(s)| Open source
Initial release| September 14, 2015 (2015-09-14)
Stable release|

October 2021 (2021-10)[1]

Repository| github.com/graphql/graphql-spec
Written in| Implementations in Java, JavaScript, Ruby, Scala, others.
Website| graphql.org

GraphQL is an open-source data query and manipulation language for APIs and a query runtime engine.

GraphQL enables declarative data fetching where a client can specify exactly what data it needs from an API. Instead of multiple endpoints that return separate data, a GraphQL server exposes a single endpoint and responds with precisely the data a client asked for.[2] Because a GraphQL server can fetch from separate data sources and present the data in a unified graph, it isn't tied to any specific database or storage engine.

## Contents

* 1 History
* 2 Design
* 2.1 Type system
* 2.2 Queries
* 2.3 Mutations
* 2.4 Subscriptions
* 2.5 Comparison to other query languages
* 3 Testing
* 4 See also
* 5 References
* 6 External links

## History

Facebook started GraphQL development in 2012 and released it as open source in 2015.[3] In 2018, GraphQL was moved to the newly established GraphQL Foundation, hosted by the non-profit Linux Foundation.[4][5]

On 9 February 2018, the GraphQL Schema Definition Language (SDL) became part of the specification.[6]

## Design

GraphQL supports reading, writing (mutating), and subscribing to changes to data (realtime updates – commonly implemented using WebSockets).[7] A GraphQL service is created by defining types with fields, then providing functions to resolve the data for each field. The types and fields make up what is known as the schema definition. The functions that retrieve and map the data are called resolvers.[8]

After being validated against the schema, a GraphQL query is executed by the server. The server returns a result that mirrors the shape of the original query, typically as JSON.[9]

### Type system

The root type of a GraphQL schema, `Query` by default, contains all of the fields that can be queried. Other types define the objects and fields that the GraphQL server can return. There are several base types, called scalars, to represent things like strings, numbers, and IDs.

Fields are defined as nullable by default, and a trailing exclamation mark can be used to make a field non-nullable (required). A field can be defined as a list by wrapping the field's type in square brackets (for example, `authors: [String]`).[10]

type Query {
currentUser: User

type User {
id: ID!
name: String!

### Queries

A GraphQL query defines the exact shape of the data needed by the client.

query CurrentUser {
currentUser {
name

Once validated and executed by the GraphQL server, the data is returned in the same shape.

"currentUser": {
"name": "Luke Skywalker"

### Mutations

A GraphQL mutation allows for data to be created, updated, or deleted. Mutations generally contain variables, which allow data to be passed into the server from the client. The mutation also defines the shape of the data that will be returned to the client after the operation is complete.

mutation CreateUser($name: String!, $age: Int!) {
createUser(userName: $name, age: $age) {
name
age

The variables are passed as an object with fields that match the variable names in the mutation.

"name": "Han Solo",
"age": 42

Once the operation is complete, the GraphQL server will return data matching the shape defined by the mutation.

"data": {
"createUser": {
"name": "Han Solo",
"age": 42

### Subscriptions

GraphQL also supports live updates sent from the server to client in an operation called a subscription. Again, the client defines the shape of the data that it needs whenever an update is made.

subscription {
newPerson {
name
age

When a mutation is made through the GraphQL server that updates the associated field, data is sent to all subscribed clients in the format setup through the subscription.

"newPerson": {
"name": "Jane",
"age": 23

### Comparison to other query languages

GraphQL does not provide a full-fledged graph query language such as SPARQL, or even in dialects of SQL that support transitive closure. For example, a GraphQL interface that reports the parents of an individual cannot return, in a single query, the set of all their ancestors.

## Testing

GraphQL APIs can be tested manually or with automated tools issuing GraphQL requests and verifying the correctness of the results. Automatic test generation is also possible.[11] New requests may be produced through search-based techniques due to a typed schema and introspection capabilities.[12]

Some of the software tools used for testing GraphQL implementations include Postman, GraphiQL, Apollo Studio, and Step CI.[13]

## See also

* Query by Example
* OpenAPI Specification
* Microservices

## References

1. ↑ "GraphQL October 2021 Release Notes". https://github.com/graphql/graphql-spec/tree/October2021.
2. ↑ "Learn GraphQL Fundamentals with Fullstack Tutorial" (in en). https://www.howtographql.com/basics/0-introduction/.
3. ↑ "GraphQL: A data query language". 14 September 2015. https://code.fb.com/core-data/graphql-a-data-query-language/.
4. ↑ "Facebook's GraphQL gets its own open-source foundation" (in en-US). TechCrunch. https://techcrunch.com/2018/11/06/facebooks-graphql-gets-its-own-open-source-foundation/.
5. ↑ "The Linux Foundation Announces Intent to Form New Foundation to Support GraphQL - The Linux Foundation" (in en-US). The Linux Foundation. 2018-11-06. https://www.linuxfoundation.org/press/press-release/intent_to_form_graphql.
6. ↑ "GraphQL SDL included in Github repository". https://github.com/facebook/graphql/pull/90/.
7. ↑ "GraphQL". Facebook. https://facebook.github.io/graphql/June2018/#sec-Language.Operations.
8. ↑ "Introduction to GraphQL | GraphQL". https://graphql.org/learn/.
9. ↑ "Execution | GraphQL". https://graphql.org/learn/execution/.
10. ↑ "GraphQL". https://spec.graphql.org/October2021/.
11. ↑ Vargas, D. M.; Blanco, A. F.; Vidaurre, A. C.; Alcocer, J. P. S.; Torres, M. M.; Bergel, A.; Ducasse, S. (2018). "Deviation Testing: A Test Case Generation Technique for GraphQL APIs". 11th International Workshop on Smalltalk Technologies (IWST): 1–9.
12. ↑ Karlsson, Stefan; Causevic, Adnan; Sundmark, Daniel (May 2021). "Automatic Property-based Testing of GraphQL APIs". 2021 IEEE/ACM International Conference on Automation of Software Test (AST) (Madrid, Spain: IEEE): 1–10. doi:10.1109/AST52587.2021.00009. ISBN 978-1-6654-3567-3. https://ieeexplore.ieee.org/document/9463000.
13. ↑

* GraphQL IDE Monorepo, GraphQL, 2023-04-25, https://github.com/graphql/graphiql/blob/aeedf7614e422c783f5cfb5e226c5effa46318fd/packages/graphiql/README.md, retrieved 2023-04-25
* "The Apollo Studio Explorer". https://www.apollographql.com/docs/graphos/explorer/.
* "Testing GraphQL APIs". https://docs.stepci.com/guides/testing-graphql.html.

## External links

0.00

(0 votes)

|

Retrieved from "https://handwiki.org/wiki/index.php?title=Software:GraphQL&oldid=3042575"