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" >
< span class = "caption-text" > Type System< / span >
< 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 = "" >
< a class = "" href = "input-types/" > Input Types< / a >
< / li >
< li class = "" >
< a class = "" href = "directives/" > Directives< / a >
< / li >
< li class = "" >
< a class = "" href = "schema/" > Schema< / a >
< / li >
< / 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" >
< a class = "" href = "../complementary-tools/" > Complementary Tools< / a >
< / 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 >
< li > Type System » < / li >
< 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 >
< p > To start using GraphQL you are expected to implement a type hierarchy and expose it as < a href = "../type-system/schema/" > Schema< / a > . < / p >
< p > In < strong > graphql-php< / strong > < code > type< / code > is an instance of internal class from
< code > GraphQL\Type\Definition< / code > namespace: < code > ScalarType< / code > , < code > ObjectType< / code > , < code > InterfaceType< / code > ,
< code > UnionType< / code > , < code > InputObjectType< / code > (or one of it's subclasses).< / p >
< p > But most of the types in your schema will be < a href = "../type-system/object-types/" > object types< / a > .< / p >
< 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 >
< p > You can also mix-and-match styles for convenience. For example:< / p >
< pre > < code class = "php" > < ?php
namespace MyApp;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
class BlogPostType extends ObjectType
{
public function __construct()
{
$config = [
'fields' => [
'body' => new ObjectType([
'name' => 'BlogPostBody',
'fields' => [
'html' => Type::string(),
'text' => Type::string(),
]
])
]
];
parent::__construct($config);
}
}
< / code > < / pre >
< h1 id = "type-registry" > Type Registry< / h1 >
2016-11-25 14:10:10 +03:00
< p > Every type must be presented in Schema by single instance (< strong > graphql-php< / strong >
2016-11-08 16:41:38 +03:00
throws when it discovers several instances with the same < code > name< / code > in schema).< / p >
< p > Therefore if you define your type as separate PHP class you must ensure that only one
instance of that class is added to schema.< / p >
< p > Typical way to do this is to create registry of your types:< / p >
< 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 >
< p > Obviously you can automate this registry as you wish to reduce boilerplate or even
introduce Dependency Injection Container if your types have other dependencies.< / p >
< p > Alternatively all methods of registry could be static if you prefer - then there is no need
to pass it in constructor - instead just use use < code > TypeRegistry::myAType()< / code > in your type definitions.< / p >
< / 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 >
< a href = "../getting-started/" class = "btn btn-neutral" title = "Getting Started" > < span class = "icon icon-circle-arrow-left" > < / span > Previous< / a >
< / 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" >
< span > < a href = "../getting-started/" style = "color: #fcfcfc;" > « Previous< / a > < / span >
< 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 >