2016-11-08 16:41:38 +03:00
<!DOCTYPE html>
<!-- [if IE 8]><html class="no - js lt - ie9" lang="en" > <![endif] -->
<!-- [if gt IE 8]><! --> < html class = "no-js" lang = "en" > <!-- <![endif] -->
< head >
< meta charset = "utf-8" >
< meta http-equiv = "X-UA-Compatible" content = "IE=edge" >
< meta name = "viewport" content = "width=device-width, initial-scale=1.0" >
< link rel = "shortcut icon" href = "../img/favicon.ico" >
2017-04-25 14:15:40 +03:00
< title > Introduction - graphql-php< / title >
2016-11-08 16:41:38 +03:00
< link href = 'https://fonts.googleapis.com/css?family=Lato:400,700|Roboto+Slab:400,700|Inconsolata:400,700' rel = 'stylesheet' type = 'text/css' >
< link rel = "stylesheet" href = "../css/theme.css" type = "text/css" / >
< link rel = "stylesheet" href = "../css/theme_extra.css" type = "text/css" / >
< link rel = "stylesheet" href = "../css/highlight.css" >
< script >
// Current page data
var mkdocs_page_name = "Introduction";
2017-04-25 14:15:40 +03:00
var mkdocs_page_input_path = "type-system\\index.md";
2016-11-08 16:41:38 +03:00
var mkdocs_page_url = "/type-system/";
< / script >
< script src = "../js/jquery-2.1.1.min.js" > < / script >
< script src = "../js/modernizr-2.8.3.min.js" > < / script >
2017-04-25 14:15:40 +03:00
< script type = "text/javascript" src = "../js/highlight.pack.js" > < / script >
2016-11-08 16:41:38 +03:00
< / head >
< body class = "wy-body-for-nav" role = "document" >
< div class = "wy-grid-for-nav" >
< nav data-toggle = "wy-nav-shift" class = "wy-nav-side stickynav" >
< div class = "wy-side-nav-search" >
< a href = ".." class = "icon icon-home" > graphql-php< / a >
< div role = "search" >
< form id = "rtd-search-form" class = "wy-form" action = "../search.html" method = "get" >
< input type = "text" name = "q" placeholder = "Search docs" / >
< / form >
< / div >
< / div >
< div class = "wy-menu wy-menu-vertical" data-spy = "affix" role = "navigation" aria-label = "main navigation" >
2017-04-25 14:15:40 +03:00
< ul class = "current" >
2016-11-08 16:41:38 +03:00
2017-04-25 14:15:40 +03:00
< li class = "toctree-l1" >
< a class = "" href = ".." > About< / a >
< / li >
2016-11-08 16:41:38 +03:00
2017-04-25 14:15:40 +03:00
< li class = "toctree-l1" >
< a class = "" href = "../getting-started/" > Getting Started< / a >
< / li >
2016-11-08 16:41:38 +03:00
2017-04-25 14:15:40 +03:00
< li class = "toctree-l1" >
2017-08-20 19:28:59 +03:00
< a class = "" href = "../complementary-tools/" > Complementary Tools< / a >
< / li >
< li class = "toctree-l1" >
< span class = "caption-text" > Type Definitions< / span >
2017-04-25 14:15:40 +03:00
< ul class = "subnav" >
< li class = " current" >
< a class = "current" href = "./" > Introduction< / a >
2016-11-08 16:41:38 +03:00
< ul class = "subnav" >
2017-04-25 14:15:40 +03:00
< li class = "toctree-l3" > < a href = "#type-system" > Type System< / a > < / li >
2016-11-08 16:41:38 +03:00
2017-04-25 14:15:40 +03:00
< li class = "toctree-l3" > < a href = "#type-definition-styles" > Type Definition Styles< / a > < / li >
2016-11-08 16:41:38 +03:00
2017-04-25 14:15:40 +03:00
< li class = "toctree-l3" > < a href = "#type-registry" > Type Registry< / a > < / li >
2016-11-08 16:41:38 +03:00
< / ul >
2017-04-25 14:15:40 +03:00
< / li >
< li class = "" >
< a class = "" href = "object-types/" > Object Types< / a >
< / li >
< li class = "" >
< a class = "" href = "scalar-types/" > Scalar Types< / a >
< / li >
< li class = "" >
< a class = "" href = "enum-types/" > Enumeration Types< / a >
< / li >
< li class = "" >
< a class = "" href = "lists-and-nonnulls/" > Lists and Non-Null< / a >
< / li >
< li class = "" >
< a class = "" href = "interfaces/" > Interfaces< / a >
< / li >
< li class = "" >
< a class = "" href = "unions/" > Unions< / a >
< / li >
< li class = "" >
2017-08-20 19:28:59 +03:00
< a class = "" href = "input-types/" > Mutations and Input Types< / a >
2017-04-25 14:15:40 +03:00
< / li >
< li class = "" >
< a class = "" href = "directives/" > Directives< / a >
< / li >
< li class = "" >
< a class = "" href = "schema/" > Schema< / a >
< / li >
2017-08-20 19:28:59 +03:00
< li class = "" >
< a class = "" href = "type-language/" > Using Type Language< / a >
< / li >
2017-04-25 14:15:40 +03:00
< / ul >
< / li >
2016-11-08 16:41:38 +03:00
2017-04-25 14:15:40 +03:00
< li class = "toctree-l1" >
< a class = "" href = "../executing-queries/" > Executing Queries< / a >
< / li >
2016-12-14 16:23:08 +03:00
2017-04-25 14:15:40 +03:00
< li class = "toctree-l1" >
< a class = "" href = "../data-fetching/" > Fetching Data< / a >
< / li >
2016-11-08 16:41:38 +03:00
2017-04-25 14:15:40 +03:00
< li class = "toctree-l1" >
< a class = "" href = "../error-handling/" > Handling Errors< / a >
< / li >
2016-11-08 16:41:38 +03:00
2017-04-25 14:15:40 +03:00
< li class = "toctree-l1" >
2017-08-20 19:28:59 +03:00
< a class = "" href = "../security/" > Security< / a >
< / li >
< li class = "toctree-l1" >
< a class = "" href = "../how-it-works/" > How it works< / a >
< / li >
< li class = "toctree-l1" >
< a class = "" href = "../reference/" > Class Reference< / a >
2017-04-25 14:15:40 +03:00
< / li >
2016-11-08 16:41:38 +03:00
< / ul >
< / div >
< / nav >
< section data-toggle = "wy-nav-shift" class = "wy-nav-content-wrap" >
< nav class = "wy-nav-top" role = "navigation" aria-label = "top navigation" >
< i data-toggle = "wy-nav-top" class = "fa fa-bars" > < / i >
< a href = ".." > graphql-php< / a >
< / nav >
< div class = "wy-nav-content" >
< div class = "rst-content" >
< div role = "navigation" aria-label = "breadcrumbs navigation" >
< ul class = "wy-breadcrumbs" >
< li > < a href = ".." > Docs< / a > » < / li >
2017-08-20 19:28:59 +03:00
< li > Type Definitions » < / li >
2016-11-08 16:41:38 +03:00
< li > Introduction< / li >
< li class = "wy-breadcrumbs-aside" >
< / li >
< / ul >
< hr / >
< / div >
< div role = "main" >
< div class = "section" >
< h1 id = "type-system" > Type System< / h1 >
2017-08-20 19:28:59 +03:00
< p > To start using GraphQL you are expected to implement a type hierarchy and expose it as < a href = "schema/" > Schema< / a > . < / p >
< p > In graphql-php < strong > type< / strong > is an instance of internal class from
< code > GraphQL\Type\Definition< / code > namespace: < a href = "object-types/" > < code > ObjectType< / code > < / a > ,
< a href = "interfaces/" > < code > InterfaceType< / code > < / a > , < a href = "unions/" > < code > UnionType< / code > < / a > , < a href = "input-types/" > < code > InputObjectType< / code > < / a > ,
< a href = "scalar-types/" > < code > ScalarType< / code > < / a > , < a href = "enum-types/" > < code > EnumType< / code > < / a > (or one of subclasses).< / p >
< p > But most of the types in your schema will be < a href = "object-types/" > object types< / a > .< / p >
2016-11-08 16:41:38 +03:00
< h1 id = "type-definition-styles" > Type Definition Styles< / h1 >
< p > Several styles of type definitions are supported depending on your preferences.< / p >
< p > Inline definitions:< / p >
< pre > < code class = "php" > < ?php
namespace MyApp;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
$myType = new ObjectType([
'name' => 'MyType',
'fields' => [
'id' => Type::id()
]
]);
< / code > < / pre >
2016-11-25 14:10:10 +03:00
< p > Class per type:< / p >
2016-11-08 16:41:38 +03:00
< pre > < code class = "php" > < ?php
namespace MyApp;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
class MyType extends ObjectType
{
public function __construct()
{
$config = [
// Note: 'name' is not needed in this form:
// it will be inferred from class name by omitting namespace and dropping " Type" suffix
'fields' => [
'id' => Type::id()
]
];
parent::__construct($config);
}
}
< / code > < / pre >
2017-08-20 19:28:59 +03:00
< p > Using < a href = "http://graphql.org/learn/schema/#type-language" > GraphQL Type language< / a > :< / p >
< pre > < code class = "graphql" > schema {
query: Query
mutation: Mutation
}
2016-11-08 16:41:38 +03:00
2017-08-20 19:28:59 +03:00
type Query {
greetings(input: HelloInput!): String!
}
2016-11-08 16:41:38 +03:00
2017-08-20 19:28:59 +03:00
input HelloInput {
firstName: String!
lastName: String
2016-11-08 16:41:38 +03:00
}
< / code > < / pre >
2017-08-20 19:28:59 +03:00
< p > Read more about type language definitions in a < a href = "type-language/" > dedicated docs section< / a > .< / p >
2016-11-08 16:41:38 +03:00
< h1 id = "type-registry" > Type Registry< / h1 >
2017-08-20 19:28:59 +03:00
< p > Every type must be presented in Schema by a single instance (< strong > graphql-php< / strong >
throws when it discovers several instances with the same < strong > name< / strong > in the schema).< / p >
2016-11-08 16:41:38 +03:00
< p > Therefore if you define your type as separate PHP class you must ensure that only one
2017-08-20 19:28:59 +03:00
instance of that class is added to the schema.< / p >
< p > The typical way to do this is to create a registry of your types:< / p >
2016-11-08 16:41:38 +03:00
< pre > < code class = "php" > < ?php
namespace MyApp;
class TypeRegistry
{
private $myAType;
private $myBType;
public function myAType()
{
return $this-> myAType ?: ($this-> myAType = new MyAType($this));
}
public function myBType()
{
return $this-> myBType ?: ($this-> myBType = new MyBType($this));
}
}
< / code > < / pre >
< p > And use this registry in type definition:< / p >
< pre > < code class = "php" > < ?php
namespace MyApp;
use GraphQL\Type\Definition\ObjectType;
class MyAType extends ObjectType
{
public function __construct(TypeRegistry $types)
{
parent::__construct([
'fields' => [
'b' => $types-> myBType()
]
]);
}
}
< / code > < / pre >
2017-08-20 19:28:59 +03:00
< p > Obviously, you can automate this registry as you wish to reduce boilerplate or even
2016-11-08 16:41:38 +03:00
introduce Dependency Injection Container if your types have other dependencies.< / p >
2017-08-20 19:28:59 +03:00
< p > Alternatively, all methods of the registry could be static - then there is no need
to pass it in constructor - instead just use use < strong > TypeRegistry::myAType()< / strong > in your
type definitions.< / p >
2016-11-08 16:41:38 +03:00
< / div >
< / div >
< footer >
< div class = "rst-footer-buttons" role = "navigation" aria-label = "footer navigation" >
< a href = "object-types/" class = "btn btn-neutral float-right" title = "Object Types" > Next < span class = "icon icon-circle-arrow-right" > < / span > < / a >
2017-08-20 19:28:59 +03:00
< a href = "../complementary-tools/" class = "btn btn-neutral" title = "Complementary Tools" > < span class = "icon icon-circle-arrow-left" > < / span > Previous< / a >
2016-11-08 16:41:38 +03:00
< / div >
< hr / >
< div role = "contentinfo" >
<!-- Copyright etc -->
< / div >
Built with < a href = "http://www.mkdocs.org" > MkDocs< / a > using a < a href = "https://github.com/snide/sphinx_rtd_theme" > theme< / a > provided by < a href = "https://readthedocs.org" > Read the Docs< / a > .
< / footer >
< / div >
< / div >
< / section >
2017-04-25 14:15:40 +03:00
2016-11-08 16:41:38 +03:00
< / div >
2017-04-25 14:15:40 +03:00
< div class = "rst-versions" role = "note" style = "cursor: pointer" >
2016-11-08 16:41:38 +03:00
< span class = "rst-current-version" data-toggle = "rst-current-version" >
2017-08-20 19:28:59 +03:00
< span > < a href = "../complementary-tools/" style = "color: #fcfcfc;" > « Previous< / a > < / span >
2016-11-08 16:41:38 +03:00
< span style = "margin-left: 15px" > < a href = "object-types/" style = "color: #fcfcfc" > Next » < / a > < / span >
< / span >
< / div >
2017-04-25 14:15:40 +03:00
< script src = "../js/theme.js" > < / script >
2016-11-08 16:41:38 +03:00
< / body >
< / html >