Coder Social home page Coder Social logo

Debugging queries about go-graphql-client HOT 4 CLOSED

hasura avatar hasura commented on May 13, 2024
Debugging queries

from go-graphql-client.

Comments (4)

hgiasac avatar hgiasac commented on May 13, 2024 1

You can use the ContructSubscription function to print and debug the output graphql string

The issue is on the datapointsList variable. You need to define the type of that input variable so the library can reflect it to the equivalent GraphQL input type.

type Foo map[string]interface{}

datapointsList := Foo{ ... }

from go-graphql-client.

zdevaty avatar zdevaty commented on May 13, 2024

For reference, this is the code that I use for the subscription:

type DataPointsSubscription struct {
	Subscription struct {
		DataPointsSubscription []struct {
			Value graphql.String `graphql:"value"`
		} `graphql:"DataPointsSubscription(datapointList: $datapointList)"`
	} `graphql:"subscription"`
}

func SubscribeDataPointValue(authToken string, datapoints []appdb.Datapoint) error {
	client := graphql.NewSubscriptionClient("...").
		WithConnectionParams(map[string]interface{}{
			"headers": map[string]string{
				"Authorization": "bearer " + authToken,
			},
		}).
		WithProtocol(graphql.GraphQLWS).
		OnError(func(sc *graphql.SubscriptionClient, err error) error {
			return fmt.Errorf("subscription client error: %v", err)
		})
	defer client.Close()

	datapointsList := []map[string]string{}
	for _, dp := range datapoints {
		datapointsList = append(datapointsList, map[string]string{
			"serialNumber":  dp.DeviceID,
			"channelNumber": dp.ChannelID,
			"datapointId":   dp.Datapoint,
		})
	}

	var sub DataPointsSubscription

	variables := map[string]interface{}{
		"datapointList": datapointsList,
	}

	str, err := client.Subscribe(&sub, variables, func(message []byte, err error) error {
		if err != nil {
			fmt.Println("Subscription error:", err)
			return err
		}
		for _, dp := range sub.Subscription.DataPointsSubscription {
			fmt.Println("Received data point value:", dp.Value)
			fmt.Printf("%+v\n", dp)
		}
		return nil
	}, nil)

	if err != nil {
		return fmt.Errorf("establishing subscription: %v", err)
	}
	fmt.Println(str)

	if err := client.Run(); err != nil {
		return fmt.Errorf("running client: %v", err)
	}
	return nil
}

from go-graphql-client.

zdevaty avatar zdevaty commented on May 13, 2024

Thank you very much for your quick response! This resolved my problem and now I know how to see the output. Maybe that would be nice to have in the readme? Shall I create a PR?

from go-graphql-client.

hgiasac avatar hgiasac commented on May 13, 2024

There is a docs section for this, but it isn't detailed. Feel free to improve the README docs.

from go-graphql-client.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.