Hello everybody, Just to ask if anybody have alrea...
# orm-help
f
Hello everybody, Just to ask if anybody have already tried to do some graphql-binding with auth (I don't even know if it's possible for now). My goal is to provide a clean way to request my graphql api on another backend, and, because otherwise is too easy, I need to provide auth token on queries/mutations and subscriptions! Thanks for your feedback on this πŸ™‚
w
grapqhl-yoga
exposes
express
as a root key like
server.express
which you can use for any kind of middleware. this will not work for subscriptions. https://github.com/LawJolla/prisma-auth0-example check that out
f
@woss Sorry if was not clear, I'm trying to consume a graphql api on a backend (and not building a graphqlapi) But don't worry, I will try by myself ^^ Just would like to know if someone have any xp with this kind of setup
w
My goal is to provide a clean way to request my graphql api on another backend,
that seems like an API to me πŸ™‚
what about
decorators
? that should work regradless is it an API or not
f
arf, I'm trying again ^^ On my backend (REST), I want to request my graphql endpoint. To explain a little bit more, the backend need to subscribe to a subscription for some business requirement. For now I have set a classic
apollo-client
but it's a little bit painfull for my collegue, he don't really like to have 50lines just for set the client (ws + http + auth links) and also to give a simple string (graphql query) to have some data. My best idea for now is to try something with
graphql-binding
to provide a "clean way" to interact with this graphql api. I just need to see how to setup this one to add the
auth-token
in the websocket and http requests πŸ˜‰
So I don't have your point about the
decorators
, but I guess it's just because this missunderstanding
s
@tim2 can you provide a few pointers?
f
Copy code
const {
  introspectSchema,
  makeRemoteExecutableSchema
} = require("graphql-tools");
const { HttpLink } = require("apollo-link-http");
const fetch = require("node-fetch");

const { Binding } = require("graphql-binding");

(async () => {
  try {
    // Create the executable schema
    const uri = "<http://localhost:6020>";
    const link = new HttpLink({ uri, fetch });
    const schema = await introspectSchema(link);
    const executableSchema = makeRemoteExecutableSchema({
      schema,
      link
    });

    // Create the binding
    const questionnaireAPI = new Binding({ schema });

    questionnaireAPI.query
      .questionnaire({ nisId: 2182 }, '{ id }')
      .then(console.log) // -> null
      .catch(console.log);
  } catch (e) {
    console.error(e);
  }
})();
My current progression (I'v disabled the auth for now, just to have only one problem ^^) no more error, but my query is not working…
Copy code
{
	questionnaire(nisId: 2182) {
		id
	}
}
My original query (working in insomnia)
t
Thanks for bringing this up @fabien0102,
graphql-binding
is exactly the right approach
We're just releasing a new version of it, here you can already try out a new generator https://github.com/graphql-binding/graphql-codegen-binding
The code you posted is definitely correct, I would just separate "making" the link from using it
f
Nice, exactly my current page before your response ^^
t
we're releasing a new api this week, maybe even today
f
Any example for injecting the header after?
(http header and connectionParam for websocket)
t
we just recommend to you right now creating your own link
ah you mean with "after", during runtime?
f
oki, I will try
yes
"after" πŸ˜‰
t
you can have a look at how we're doing that in the prisma binding https://github.com/graphcool/prisma-binding/blob/master/src/Prisma.ts#L24
f
Perfect πŸ‘Œ
Just to try to resume, you give a permanent app token on the binding creation, and after you deal with auth with a classic apollo link (as in any react app)?
Copy code
$ graphql-codegen-binding -s src/schema.graphql -g javascript -t qna.js
(node:12284) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1
): TypeError: Cannot read property 'split' of undefined
(node:12284) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In
the future, promise rejections that are not handled will terminate the Node.js process with
 a non-zero exit code.
😒
Same with
typescript
as generator and with an
endpoint
instead of a local schema
It's due to the
headers
parsing πŸ˜‰
no headers set ->
[undefined]
->
[undefined].map(h => h.split())
-> Boom!
@tim2 https://github.com/graphql-binding/graphql-codegen-binding/issues/3 I don't have the time now to fix this, so I made an issue to not forget πŸ˜‰
t
Oh ok, thanks for finding the issue & documenting it!
f
Totally normal ;) Meeting Time for me... I will continue tomorrow ;) thx for your support
l
Since it's an Apollo Link, you should be able to add setContext with the authentication header
f
I saw πŸ˜‰ Thanks for support BTW, @tim2 Ideally I would like something more abstract: 1.
graphql-codegen-binding ... -t MyService.js
2.
Copy code
import { MyService } from "./MyService"

const myService = new MyService({endpoint: "", secret: ""})
// start playing \o/
Is it something in the roadmap for this kind of implementation or should I start to fork 😁 ?
t
@fabien0102 you have the exact right timing! I'm releasing some stuff around that, especially the new github example binding. That will be available in about 30-60min, stay tuned!
f
Oh yeah! πŸ‘Œ
t
f
Nice! I finish my "real tasks" and play with this ^^