Want to create graphql codegen #graphql-codegen/typescript-apollo-angular
codegen.yaml
schema: http://localhost:3031/graphql
documents: "src/**/*.gql"
generates:
./src/models/graphql.ts:
plugins:
- typescript
- typescript-operations
- typescript-apollo-angular
Generated Gql query
export const MyQueryDocument = gql`
query MyQuery($input: String!) {
MyQuery(
input: $input
) {
name
id
type
}
}`;
When I run mocha test case getting error
Working fine only when I remove gql from the above query (I mean a simple string const to gql)
Related
I'm not sure if passing the schema name as an argument in the GraphQL query will work for JoinMonster or not. Following is the query example:
{
configuration_queries(table: 'xyz', where: 'some condition') {
data {
col1
col2
}
}
}
so here it is expected to create a dynamic query on "xyz" table using JoinMonster.
Yes, it's possible to pass schema details dynamically to GraphQL queries. There are a few different ways to do this, but one approach would be to use the graphql-tools package:
Install the graphql-tools package:
npm install --save graphql-tools
Write your GraphQL query, using the graphql-tools syntax for dynamic schema details:
const query = `
query($schema: String!) {
joinMonster(sql: $schema)
}
`;
Call the graphql function, passing in your query and dynamic schema details:
const schemaDetails = `
SELECT * FROM users
JOIN posts ON users.id = posts.user_id
`;
graphql(query, { schema: schemaDetails })
.then((result) => {
// handle results...
})
.catch((error) => {
// handle errors...
});
I have a GraphQL server built with apollo-server and i am getting this error. The query works correctly on apollo graphql studio, but I get the error when I try fetching the data in my React application.
const GET_VEHICLE_MAKES = gql`
query {
vehicleMakes {
id
make
}
}
`;
const { loading, error, data } = useQuery(GET_VEHICLE_MAKES);
I have 2 graphql mutations : mutation1 and mutation2.
mutation2 uses mutation1 result (field id).
I'd like to create a seed and then run it in graphql playground.
My issue is that i can't use the id field in mutationResult in my other mutation.
so - this line is an error : id: mutationResult.id
Is there a way to do this?
I know i can create a js script for this, or import apollo client, but i'd like to use gql seed file instead.
AND YES - i'm aware of this issue : https://github.com/graphql/graphql-js/issues/462
mutation {
mutationResult: mutation1(
params: {
email: "test#gmail.com"
name: "test"
}
) {
id
}
mutation2(
params: {
id: mutationResult.id // THIS IS AN ERROR
}
)
}
I am trying to use variables with GraphQL queries but the browser is responding with bad request.
here is my code
return this.apollo.query({
query: gql `query Project($projectId: String!){
project( where: { id: $projectId } )
{
id
initials
}
}`,
variables: { projectId: pid },
fetchPolicy: 'network-only'
})
for some reason its not picking the project variable in the query. when i replace the variables with hard-coded values it works fine.
can you please help me to understand why.
The issue with the code was with the data type of $projectId. this is an actual id in the database(i.e prisma in my case). so I changed the code to :
return this.apollo.query({
query: gql `query Project($projectId: ID!){
project( where: { id: $projectId } )
{
id
initials
}
}`,
variables: { projectId: pid },
fetchPolicy: 'network-only'
})
and its working now.
I found graphql playground very helpful and providing variables in the bottom of the screen. that way you can debug the code faster
I am using apollo-cache-inmemory, apollo-client, react-apollo.
My local state contains a users array like so: -
users: [{
__typename: "User",
userId: "hashid1",
...
},
{
__typename: "User",
userId: "hashid2",
...
}]
Now I can obviously run a simple query to retrieve all the users from the local state: -
import gql from "graphql-tag"
export default gql`{users #client {userId}}`
However, what I would like to do is to be able to query the users array directly, passing variables like so: -
const userDetails = await client.query({ query: USER_DETAILS, variables: {id: "hashId1"}})
Is it possible to run this query without using a resolver? I have attempted the following but { data } returns as null: -
export default gql`query user($id: String!) {users(userId: $id) #client {userId}}`
I already use resolvers and can easily write one to take care of this issue but I am wondering if there it is possible to perform this task without one?
It looks like you're looking for some magic ;)
You must write customization code (overwrite default resolver - return all records) to have a customized behavior (return data filtered by your criteria). That should be obviuos.
There is no default/ready/built in searching/filtering syntax in graphql - therefore, there is no default behaviours for them in apollo-client (no matter local/remote server/data). It is up to you to implement what you need.