在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):gazuntype/graphQL-client-unity开源软件地址(OpenSource Url):https://github.com/gazuntype/graphQL-client-unity开源编程语言(OpenSource Language):C# 100.0%开源软件介绍(OpenSource Introduction):graphQL-client-unitygraphQL-client-unity is, as the name implies, a graphQl client for the Unity game engine. Its major aim is to simplify the creation of graphQl queries and make graphQl features like subscriptions as straightforward as possible. How it worksWhen imported, graphQl-client-unity allows the creation of queries within the Unity Editor. The client utilizes one of graphQl's amazing features, Introspection, to get all the queries, mutations, subscriptions, types and arguments from the graphQl schema. With this information, an easy-to-use editor layout allows the user to create queries by simply selecting from the available options within the editor. Each query created is stored in a scriptable object and can be used as many times as needed by simple calling one line of code. UnityWebRequest request = await exampleApi.Post("QueryName", GraphApi.Query.Type.Query); The client also utilizes different Events that can be subscribed to by any function. The Events are called when a request is completed, when data is gotten from a subscription and other useful cases. How to useImport UnityPackageDownload the unitypackage file and import it into your Unity project. Create an API ReferenceAn API reference is a Scriptable Object that stores all the data relating to an API. For instance, if the API we intend to query is the Pokemon GraphQl API, within Unity, we would create an API Reference and point it to the url of the Pokemon GraphQl API. This API Reference will contain all the queries, mutations and subscriptions we wish to make pertaining to the Pokemon GraphQl API. To create an API Reference, simply right click in your Asset folder, go to Create -> GraphQLClient -> API Reference. This would automatically create a new API Reference. Name it appropriately, put the url endpoint of the GraphQl API and click Introspect to begin creating queries. Note: You'll need to Introspect every time to make changes to your GraphQl API to fetch the current schema. Create a Query, Mutation or SubscriptionTo create a query, mutation or subscription is very intuitive and the processes are the same. Simply select Create Query (or Mutation, Subscription depending on your goal). Give the Query a name and pick the query from the dropdown menu displayed. After selecting the query you want to create, click confirm query and you can begin adding fields and subfields to the query. When you create a Query within the Editor, a Query object is created in the API Reference and this object houses all the information about that particular query. Preview a QueryYou can preview a query created to see how it looks as text. This is done simply by clicking the Preview Query button at the bottom of the query. Use the Edit Query button to go back to editing the query Using the API ReferenceTo use an API reference to actually query APIs, you need to reference it within a script using GraphQlClient.Core;
public GraphApi pokemonReference; This allows you to drag and drop the API reference into the public field created in the Inspector. With the reference, you can query the API easily using the public async void GetPokemons(){
UnityWebRequest request = await pokemonReference.Post("GetAllPokemons", GraphApi.Query.Type.Query);
} The Post function returns a UnityWebRequest object and data gotten from the UnityWebRequest object can be gotten by string data = request.downloadHandler.text; This data is in JSON format and can easily be parsed using a tool like Unity's in-built JsonUtility class or third party JSON parsers like JSON. Net For Unity Note: the Post function has three overloads. Post(Query query) //takes in a Query object. this can be gotten by using GetQueryByName(string queryName, Query.Type type)
Post(string queryString) //takes in the query as a string input. this should be used if the query can't be formed in the editor
Post(string queryName, Query.Type type) //takes in the name of the query (created in the editor) and the type of query. Setting Query InputDue to the dynamic nature of query inputs, you cannot set them within the editor. Therefore input objects have to be created and the function public async void CreateNewUser(){
//Gets the needed query from the Api Reference
GraphApi.Query createUser = userApi.GetQueryByName("CreateNewUser", GraphApi.Query.Type.Mutation);
//Converts the JSON object to an argument string and sets the queries argument
createUser.SetArgs(new{objects = new{id = "idGiven", name = "nameGiven"}});
//Performs Post request to server
UnityWebRequest request = await userApi.Post(createUser);
} Of course Alternatively, you can use the overload function NOTE: The input object must be just like the input object in the APIs schema.
For the example above, the query expected an input in this form Authentication/AuthorizationFor authentication and authorization, the API reference has a function called SubscriptionsA subscription is created the same way as a query or a mutation. The only difference is instead of calling private void OnEnable(){
OnSubscriptionDataReceived.RegisterListener(DisplayData);
}
private void OnDisable(){
OnSubscriptionDataReceived.UnregisterListener(DisplayData);
}
public void DisplayData(OnSubscriptionDataReceived subscriptionDataReceived){
Debug.Log(subscriptionDataReceived.data);
} EventsFor easy use, a couple of events have been created that can be subscribed to by functions. These events are called when specific things happen. A list of events created for the graphql-client are shown below. An Event can also contain some data relating to the event.
You can subscribe to these events by simply calling private void OnEnable(){
OnRequestEnded.RegisterListener(DisplayData);
}
private void OnDisable(){
OnRequestEnded.UnregisterListener(DisplayData);
}
public void DisplayData(OnRequestEnded dataReceived){
if (dataReceived.success){
Debug.Log(dataReceived.data);
}
else{
throw dataReceived.exception;
}
} NOTE: The function that subscribes to an Event must contain the Event class as an argument. Notice above with |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论