Graphql not working with asp .net core web api project


#1

I am struggling to incorporate Graphql in my ASP.Net Core Web API project. My API is able to link with cloud and get the data. What we need is to utilize the seamless capability of Realm by refreshing the data on its own.

I referred following link https://realm.io/docs/realm-object-server/latest/api/graphql/classes/graphqlconfig.html but was not able to use GraphQLConfig.


#2

Why do you need to use the GraphQL API as opposed to just using the .NET SDK? The documentation that you’ve linked is for a javascript library and unless you have a front end project to compliment your web api, it’s unlikely you can use it.


#3

Thanks! I got it! I’ve Asp.Net MVC web application, what changes are needed to make it work? I need to understand React for this?


#4

Do you need to read/write data from the front end (your js app) or the back end (the .NET app)? You don’t need to understand/use React to use the GraphQL API - they’re not tied to your front end framework. You can use the minimal sample in the docs and modify it with your data to get started. Note that this is a purely javascript library - it doesn’t interop at all with any of the MVC magic, so you’ll need to invoke it and consume it from your web pages (you can of course read/send data back to your .NET backend, but it’s up to you to do it).


#5

My .Net core web api is ready that interacts with Realm cloud. I’m working on a solution where my web consumes the developed api.
Now, I created web application in React that is consuming my api where any change happening in Realm is not reflected in my web page. Now, confusion is do I need to use websockets or GraphQL? As, I understood from documentation is that to implement GraphQL and Apollo Client? Is there a way on API/server side?


#6

You can use SignalR or something similar to push changes from your backend to the frontend. This is not really related to Realm.


#7

How the API will know that data changed in Realm?


#8

If you’re interested in observing changes on your backend (.NET app), you can use the Realm .NET SDK. You can read more on subscribing for notifications in the docs. If you want to do it in the front end without going through your own backend, you can use the GraphQL API to communicate directly with the Realm Object Server. Finally, if you want to be notified of changes on your backend and as a response to that, update your front end, you need something like SignalR - when a change event is emitted from Realm, you use a websocket to tell your UI that something has changed and it needs to redraw in response.


#9

You are awesome!!! will go with approach 1st and 3rd. thanks!!


#11

Receiving 400 (Bad Request) error when trying to connect to cloud to get data
const credentials = Credentials.usernamePassword("–", “–”, false);
const user = await User.authenticate(credentials, “https://nsnewinstance.us1a.cloud.realm.io”);
const config = await GraphQLConfig.create(user,"/ExampleRealm01");
const httpLink = ApolloLink.from([config.authLink,
// Note: if using node.js, you’ll need to provide fetch as well.
new HttpLink({ uri: config.httpEndpoint })
]);
this.client = new ApolloClient({
link: httpLink ,
cache: new InMemoryCache()
});

    const response = await this.client.query({
        query: gql`
                query {
                     Product {
                            ProductId
                            ProductName
                            Price
                            Category
                          }
                }
            `
    })

In DevTools
POST https://nsnewinstance.us1a.cloud.realm.io/graphql/%2FExampleRealm01 400 (Bad Request)

What can be wrong?


#12

The body of the 400 response will include information about what went wrong. In this case though, it looks like the query node is invalid. You can read our docs for more information about how the node names are generated, but essentially, the class name is camelcased and pluralized, so if you have a Product class, that becomes a products node.


#13

We just released Realm Studio 3.5.0 that includes a built-in GraphiQL explorer. It’s an extremely useful tool that allows you to run your queries and explore the results in a web-based interface. It has autocompletion built-in and validates the schema, making it super easy to see which API are available and make sure you don’t have typos. Once you’re happy with how the query looks like, you can just paste it in the gql tag.


#14

Downloaded!! Thanks !
My Reactive page is able to get the data from Realm cloud using GraphQL and ApolloClient.
Now next challenge is “Subscription”…
Subscription URL - url: "wss://nsnewinstance.us1a.cloud.realm.io/graphql/%2FExampleRealm01"
Added following code -
const credentials = Credentials.usernamePassword("—", “—”, false);
const user = await User.authenticate(credentials, “https://nsnewinstance.us1a.cloud.realm.io”);
const config = await GraphQLConfig.create(user,"/ExampleRealm01");
const httpLink = ApolloLink.from([config.authLink,
new HttpLink({ uri: config.httpEndpoint })]);

const subscriptionLink = new WebSocketLink({uri: config.webSocketEndpoint,
options: {connectionParams: config.connectionParams }
});
const link = ApolloLink.split(
({ query }) => {
const { kind, operation } = getMainDefinition(query);
return kind === “OperationDefinition” && operation === “subscription”;
},
subscriptionLink,
httpLink
);

        this.client = new ApolloClient({
            link: link,
            cache: new InMemoryCache()
        });
    
    const response = await this.client.query({
        query: gql`
                query {
                     products {
                            ProductId
                            ProductName
                            Price
                            Category
                          }
                }
            `
    });

So, if more products get added or deleted on cloud, as websocket is opened, so React page should get updated. But this is not happening.

Thanks again for all the help and guidance.


#15

You’re executing a query, not a subscription. To subscribe you need something like:

const observable = await client.subscribe({
    query: gql`
        subscription {
            products {
                ProductId
                ProductName
            }
        }
    `
});

observable.subscribe({
    next(data) {
        console.log(data.data.products);
    },
    error(value) {
        console.error(value);
    }
});

But that’s an no longer directly related to Realm, so it’s best to look into the Apollo docs.


#16

Thanks ! In our POC, we showcased RealmDB with both mobile and web, went well. Now, management needs to know how can we make RealmDB show real time replication with SQL or Oracle. Starting up with this.

Thanks again