Created
July 11, 2023 13:13
-
-
Save AliNaraghiA/b4497f274718f9c3c984798dfe8118a5 to your computer and use it in GitHub Desktop.
store post meta in WPGraphQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'graphql_register_types', function() { | |
register_graphql_field( 'Post', 'color', [ | |
'type' => 'String', | |
'description' => __( 'The color of the post', 'wp-graphql' ), | |
'resolve' => function( $post ) { | |
$color = get_post_meta( $post->ID, 'color', true ); | |
return ! empty( $color ) ? $color : 'blue'; | |
} | |
] ); | |
} ); | |
export default { | |
async asyncData({ app }) { | |
const { data } = await app.apolloProvider.defaultClient.query({ | |
query: gql` | |
{ | |
posts { | |
nodes { | |
id | |
link | |
color | |
} | |
} | |
} | |
`, | |
}); | |
const posts = data.posts.nodes; | |
console.log(posts[0].color); // Access the color field of the first post | |
return { posts }; | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment