Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mutation for authorized users #204

Closed
kuraiconnect opened this issue Sep 23, 2022 · 6 comments
Closed

Mutation for authorized users #204

kuraiconnect opened this issue Sep 23, 2022 · 6 comments
Assignees

Comments

@kuraiconnect
Copy link

kuraiconnect commented Sep 23, 2022

It is not possible to add a mutation that is only available to authorized users.

I add new mutation in mutations folder api/mutations/addSeller.gql
it will only be available to authorized users. But when i added mutation I get an error:
Cannot query field "addSeller" on type "Mutation".

i think problem with mutation because it will be available if add headers option:

  • x-hasura-user-role with value user_role
  • Authorization with value Bearer user_token

how i can get access to this mutation when build my application?

@Diizzayy Diizzayy self-assigned this Sep 23, 2022
@Diizzayy
Copy link
Owner

@kuraiconnect is this error occurring at start time or when you attempt to execute the mutation?

@kuraiconnect
Copy link
Author

@Diizzayy at start time

@kuraiconnect
Copy link
Author

Maybe I can show this problem for find solution?

@Diizzayy
Copy link
Owner

at start time

@kuraiconnect The problem here is that the graphql codegen is lacks the proper authorization, hence it's unaware of the addSeller mutation, The two main workarounds that come to mind is 1. manually linking to the schema and 2. authorizing the codegen with a token that has access to all the roles ( a PR is on the way that makes this possible ).

@kuraiconnect
Copy link
Author

@Diizzayy thx for your answer. I try it later.

Now I use module for this and do not have problem with this mutation.

i add two dependencies on my project
@apollo/client and @vue/apollo-composable

after i add this plugin on my project

import { defineNuxtPlugin } from '#app'
import { ApolloClient, InMemoryCache } from '@apollo/client/core'
import { DefaultApolloClient } from '@vue/apollo-composable'

export default defineNuxtPlugin((nuxtApp) => {
  const apolloClient = new ApolloClient({
    cache: new InMemoryCache(),
    uri: 'http:https://localhost:8080/v1/graphql',
  })
  nuxtApp.vueApp.provide(DefaultApolloClient, apolloClient)
})

and use mutation in component

const { mutate: createCompanyBuyer, loading } = useMutation(addSeller, {
  context: {
    headers: {
      'x-hasura-user-role': 'role',
      Authorization: `Bearer ${token}`,
    },
  },
})

and it's work for me, but I would like to use only your module later because it is very convenient and laconic

@Diizzayy
Copy link
Owner

@kuraiconnect Apologies for the delay here.

v0.1.25 was recently released as per #211, This allows you to specify additional headers that would only be passed to the GraphQL code generator. As mentioned in my previous response, the issue here is that the code generator is unathorized and hence unaware of the addSeller mutation. The solution here would be to provide the proper token and x-hasura-user-role headers as seen below.

export default defineNuxtConfig({
  modules: ['nuxt-graphql-client'],

  runtimeConfig: {
    public: {
      'graphql-client': {
        clients: {
          default: {
            host: 'https://api.spacex.land/graphql',
            token: 'super_secret', // token type is bearer by default
            codegenHeaders: {
              'x-hasura-user-role': 'role'
            }
          }
        }
      }
    }
  }
})

Given the exact configuration above, the following headers would be applied to the graphql code generator:

{
  "Authorization": "Bearer super_secret",
  "x-hasura-user-role": "role"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants