Search Unity

Success implementing GraphQL in Unity3D?

Discussion in 'Scripting' started by peekaTroo, Jun 30, 2017.

  1. peekaTroo

    peekaTroo

    Joined:
    Jan 20, 2017
    Posts:
    1
    Hi there, we're new to this community, but are kicking off a new project that will use the Unity 3D engine. Because we'll be building multiple apps in addition to the Unity app all around the same data repository, we will be launching a sub-project to create a GraphQL server that will service all these apps.

    From my preliminary searches, I've found graphql-dotnet, but I'd like to see if anyone here has actually implemented a GraphQL client within their game or application built on Unity and what that experience was like. Does Unity even allow inclusion of third-party .NET libraries (sorry we're new to this)?

    Note: we will be implementing the actual data fetches using a traditional REST API behind the GraphQL server, so we can always use that as a fallback. However, we really want to get all projects across the company using GraphQL for the benefits that that will convey.

    Thanks.
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Looks like it requires .NET Core so it wouldn't work out of the box but you might be able to get it to compile if you stripped features.

    Yup! Compile them to a DLL and put them in your project somewhere. https://docs.unity3d.com/Manual/Plugins.html

    Honestly - this is a better design anyway. I certainly wouldn't want n number of random clients directly accessing my data. Expose how you want your data accessed and packaged via an explicit REST API and let your clients use it.
     
  3. carlmyerflor

    carlmyerflor

    Joined:
    Sep 30, 2014
    Posts:
    1
    rafaelnsantos and nelsonlarocca like this.
  4. rafaelnsantos

    rafaelnsantos

    Joined:
    Dec 9, 2016
    Posts:
    1
  5. mrbigheaddd

    mrbigheaddd

    Joined:
    Nov 11, 2016
    Posts:
    7
    Masaco likes this.
  6. Deleted User

    Deleted User

    Guest

    You can actually use the default WWW and JsonUtility of Unity for GraphQL requests. assuming you have graphql arguments builder to setup your string or you arrange the string manually, you can do the following:

    Code (CSharp):
    1. // build your graph args
    2. string graphArgs = ProcessMyGraphArgs();
    Code (CSharp):
    1. // then encode the graph args to UTF8 as www post data.
    2. byte[] bytes = Encoding.UTF8.GetBytes(graphArgs);
    Code (CSharp):
    1. // since using Coroutine is not good practice for asynchronous operations, I use [URL='https://github.com/neuecc/UniRx']UniRx[/URL] for these type of operations.
    2. // Then parse the result data with the Unity's default Json utility.
    3. ObservableWWW.Post(Info.GraphURL, bytes)
    4. .Subscribe(_ =>
    5. {
    6. [INDENT]ResultData data = JsonUtility.FromJson<ResultData>(_);[/INDENT]
    7. },
    8. _ => Debug.LogErrorFormat("Graph error! Args:{0} Result:{1}\n", graphArgs, _));
    9.  
     
    Last edited by a moderator: Apr 9, 2018
  7. waldgeist

    waldgeist

    Joined:
    May 6, 2017
    Posts:
    388
  8. mrbigheaddd

    mrbigheaddd

    Joined:
    Nov 11, 2016
    Posts:
    7
  9. Nickjd331

    Nickjd331

    Joined:
    Aug 22, 2014
    Posts:
    29
    Hi mrbigheaddd,
    I tried using your tool but it errors out at the introspect section, I think probably due to the requirement to authenticate. Is this something you can add?

    Edit - My bad, I see you support authentication. So not sure why it fails there.
     
    Last edited: Aug 26, 2020
  10. MathewsBabuWest

    MathewsBabuWest

    Joined:
    Sep 29, 2020
    Posts:
    1
  11. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,982
    Can you update your client to provide a way that works with WebGL? Yours uses threading which does not work on all WebGL targets
     
  12. Risine

    Risine

    Joined:
    Dec 10, 2009
    Posts:
    154
    I'm trying to use use graphQL-client-unity with bitquery.io, but it does not seem to work. For testing purposes, I get a working query from bitquery.io, and paste it in unity querystring before calling a UnityWebRequest.
    I have this error :
    {"errors":[{"message":"Field 'ethereum' doesn't accept argument 'arguments'","locations":[{"line":2,"column":16}],"path";..
     
  13. vogler

    vogler

    Joined:
    Jul 2, 2021
    Posts:
    2
    Hi, I have created a Unity Asset for GraphQL ... but it is still in the publishing queue... and it will take another weeks. If you need it before, get in touch with me. It works also with WebGL - if you just use HTTP (Websocket does not work in WebGL). I also tried a simple query with BitQuery.IO. Worked fine. Screenshot 2021-09-23 at 21.03.13.png