GraphQL
![]() | This article has multiple issues.Please helpimprove itor discuss these issues on thetalk page.(Learn how and when to remove these template messages)
|
![]() | |
Original author(s) | Meta Platforms |
---|---|
Developer(s) | Open source |
Initial release | September 14, 2015 |
Stable release | October 2021[1]
|
Repository | github |
Written in | Implementations inJava,JavaScript,Ruby,Scala,others. |
Website | graphql |
GraphQLis a dataqueryandmanipulationlanguage forAPIs,that allows aclientto specify what data it needs ( "declarativedata fetching "). A GraphQL server can fetch data from separate sources for a single client query and present the results in a unifiedgraph,[2]so it is not tied to any specificdatabaseor storage engine.
The associated GraphQLruntime engineisopen-source.
History
[edit]Facebookstarted 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-profitLinux Foundation.[4][5]
On 9 February 2018, the GraphQL Schema Definition Language became part of the specification.[6]
Many popular public APIs adopted GraphQL as the default way to access them. These include public APIs of Facebook,GitHub,Yelp,Shopifyand Google Directions API.[7]
Design
[edit]GraphQL supports reading, writing (mutating), and subscribing to changes to data (realtime updates – commonly implemented usingWebSockets).[8]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 theschema definition.The functions that retrieve and map the data are calledresolvers.[9]
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 asJSON.[10]
Type system
[edit]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 asnullableby default, and a trailingexclamation markcan be used to make a field non-nullable (required). A field can be defined as a list by wrapping the field's type insquare brackets(for example,authors: [String]
).[11]
typeQuery{
currentUser:User
}
typeUser{
id:ID!
name:String!
}
Queries
[edit]A GraphQL query defines the exact shape of the data needed by the client.
queryCurrentUser{
currentUser{
name
age
}
}
Once validated and executed by the GraphQL server, the data is returned in the same shape.
{
"currentUser":{
"name":"John Doe"
"age":23
}
}
Mutations
[edit]A GraphQL mutation allows for data to be created, updated, or deleted. Mutations generally containvariables,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.
mutationCreateUser($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
[edit]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
[edit]GraphQL does not provide a full-fledgedgraphquery language such asSPARQL,or even indialects of SQLthat supporttransitive 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
[edit]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.[12]New requests may be produced through search-based techniques due to a typed schema and introspection capabilities.[13]
Some of the software tools used for testing GraphQL implementations includePostman,GraphiQL, Apollo Studio, GraphQL Editor, and Step CI.[14]
See also
[edit]References
[edit]- ^"GraphQL October 2021 Release Notes".GitHub.
- ^"Learn GraphQL Fundamentals with Fullstack Tutorial".howtographql.Retrieved25 April2023.
- ^"GraphQL: A data query language".14 September 2015.
- ^"Facebook's GraphQL gets its own open-source foundation".TechCrunch.Retrieved7 November2018.
- ^"The Linux Foundation Announces Intent to Form New Foundation to Support GraphQL".The Linux Foundation.6 November 2018.Retrieved17 March2023.
- ^"GraphQL SDL included in Github repository".GitHub.
- ^"Popular public APIs that use GraphQL".Frontendeng.dev.25 July 2023.
- ^"GraphQL".facebook.github.io.Facebook.Archived fromthe originalon 18 July 2018.Retrieved4 July2018.
- ^"Introduction to GraphQL".graphql.org.Retrieved25 April2023.
- ^"Execution".graphql.org.Retrieved25 April2023.
- ^"GraphQL".spec.graphql.org.Retrieved25 April2023.
- ^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.
- ^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. pp. 1–10.arXiv:2012.07380.doi:10.1109/AST52587.2021.00009.ISBN978-1-6654-3567-3.S2CID229156477.
- ^
- GraphQL IDE Monorepo,GraphQL, 25 April 2023,retrieved25 April2023
- "The Apollo Studio Explorer".Apollo Docs.Retrieved25 April2023.
- "GraphQL Editor API Console".GraphQL Editor Docs.Retrieved2 September2023.
- "Testing GraphQL APIs".Step CI Documentation.Retrieved8 January2023.
External links
[edit]