在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):cfpinto/graphql开源软件地址(OpenSource Url):https://github.com/cfpinto/graphql开源编程语言(OpenSource Language):PHP 100.0%开源软件介绍(OpenSource Introduction):graphqlA simple yet powerful GraphQL query builder Change logA lot of rewriting was done on this version particularly at code organization and entity management. Whit this level of rewriting keeping backwards compatibility is tricky but did my best to do so. There are a few deprecation notices that will be dropped next version:
How it worksWriting GraphQL queries can be a painful process. With GraphpQL query builder write PHP and get GraphQL ExamplesWhen finding a Hero to be his sidekick, one finds himself overwhelmed with the number of Heroes out there. There are all sort of Heroes so lets list them all Fields$hero = new GraphQL\Graph('hero');
echo $hero->use('name')
->friends
->use('name')
->root()
->query(); will generate
ArgumentsA Hero will have many friends which can make it hard to walk through, it would be great limit the Hero's friends to 2 $hero = new GraphQL\Graph('hero');
echo $hero->use('name')
->friends(['first'=>2])
->use('name')
->root()
->query(); will generate
Going back the treeSometimes you might need to know more about this Hero, like when you want to know a hero friends and costumes. We then need to go back in the Hero tree. For that we'll use $hero = new GraphQL\Graph('hero');
echo $hero->use('name')
->friends(['first'=>2])
->use('name')
->prev()
->costumes
->color
->root()
->query(); will generate
Inline FragmentsSometimes you don't quite know the type of hero you looking for. Maybe you looking a flying Hero, maybe a Strong Hero $hero = new GraphQL\Graph('hero');
echo $hero->use('name')
->on('FlyingHero')
->use('hasCape')
->prev()
->on('StrongHero')
->use('strengthLevel')
->prev()
->friends(['first'=>2])
->use('name')
->prev()
->costumes
->color
->root()
->query(); Will generate
AliasesFor the element of surprise, you might need to name some of the hero's properties differently; You might want to call friends as partners_in_good or name as call_me_this $hero = new GraphQL\Graph('hero');
echo $hero->use('name')
->alias('call_me_this', 'name')
->friends(['first'=>2])
->alias('partners_in_good')
->use('name')
->prev()
->costumes
->color
->root()
->query(); will generate
FragmentsSorry have no super hero narrative from here :D . sticking to good old technical explanation To use fragments declare the fragment as you would a graph and then use it within a $fragment = new GraphQL\Fragment('properties', 'Hero');
$fragment->use('id', 'age');
$hero = new GraphQL\Graph('hero');
echo $hero->use('name', $fragment)->query();
echo $fragment->query(); will generate
VariablesThe use of variables feels less necessary because we're using PHP to build the query. Still... $variable = new GraphQL\Variable('name', 'String');
$hero = new GraphQL\Graph('hero', ['name' => $variable]);
echo $hero->use('name')->query(); will generate
Meta fieldsyou can also use meta fields the same way you would request a property $variable = new GraphQL\Variable('name', 'String');
$hero = new GraphQL\Graph('hero', ['name' => $variable]);
echo $hero->use('name', '__typename')->query(); will generate
Which can also be aliased $variable = new GraphQL\Variable('name', 'String');
$hero = new GraphQL\Graph('hero', ['name' => $variable]);
echo $hero->use('name', '__typename')
->alias('type', '__typename')
->query(); will generate
MutationsAfter you chose your Hero and he takes you as his sidekick he will let you do some help him with some of his daily routine. He might even let you choose his costume color. How cool is that? $mutation = new GraphQL\Mutation('changeHeroCostumeColor', ['id' => 'theHeroId', 'color'=>'red']);
$mutation
->hero
->use('name')
->costumes
->use('color')
->root()
->query(); Will generate
With variables $mutation = new GraphQL\Mutation('changeHeroCostumeColor', ['id' => new GraphQL\Variable('uuid', 'String', ''), new GraphQL\Variable('color', 'String', '')]);
$mutation
->hero
->use('name')
->costumes
->use('color')
->root()
->query(); Will generate
Build Status |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论