Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
440 views
in Technique[技术] by (71.8m points)

typescript - GraphQL mutation for 2 entities with parent-child relation returns old value for second entity

I'm using apollo server.

While mutating data I need to update 2 tables: location and address.

End of resolver function:

const locationUpdated = await db.location.updateLocation(user.id, location);
const addressUpdated = await db.address.updateAddress(user.id, location.address);
return { error: null, location: { ...locationUpdated, address: addressUpdated } };

Using GraphQL Playground:

mutation {
  updateLocation(
    location: {
      id: 1
      label: "label12"
      address: { 
        city: "city12"
      }
    }
  ) {
    error
    location {
      label
      address {
        city
      }
    }
  }
}

As a result I have new location.label but old location.address.city:

{
  "data": {
    "updateLocation": {
      "error": null,
      "location": {
        "label": "label12",
        "address": {
          "city": "city11",
        }
      }
    }
  }
}

Database is updated correctly. Looks like server uses cache?

question from:https://stackoverflow.com/questions/65901248/graphql-mutation-for-2-entities-with-parent-child-relation-returns-old-value-for

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can try the following two ways

  1. override the server caching setting. Ref
const server = new ApolloServer({
  // ...
  cacheControl: {
    defaultMaxAge: 0,
  },
}));
type Post @cacheControl(maxAge: 240) {
  id: Int!
  title: String
  author: Author
  votes: Int @cacheControl(maxAge: 30)
  comments: [Comment]
  readByCurrentUser: Boolean! @cacheControl(scope: PRIVATE)
}

type Comment @cacheControl(maxAge: 1000) {
  post: Post!
}
  1. updating the id. Ref
updateLocation(
    location: {
      id  //from the doc, I saw that the id must be updated as well, you can try take out the hard code `1`
      label: "label12"
      address: { 
        city: "city12"
      }
    }
  )

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...