为什么我的数组项没有传入我的函数foreach PHP for GraphQL?

My items from my array seem to not be passed into the my 'resolve' => function. Right now the function returns fail. If I replace $meta with an array item then it works. Im trying to fetch meta values for GraphQL and dont want to make a function per field.

            add_action( 'graphql_register_types', function() {
                $metas = array('phone', 'city', 'state', 'zip');
                foreach($metas as $meta){
                    register_graphql_field( 'Location', $meta, [
                        'type' => 'String',
                        'description' => __( 'The post data', 'wp-graphql' ),
                        'resolve' => function($post, $meta) {
                        $GQL_data = get_post_meta( get_the_ID(), $meta, true);
                        return ! empty( $GQL_data  ) ? $GQL_data  : 'fail';
                        }
                    ] );
                }
            });

Expected GraphQL output:

"phone": "123-123-1234",

"city": "Los Angeles",

"state": "CA",

"zip": "99922"

currently all items have a value of: "fail"

The problem is your resolve function when trying to pass a variable into the scope of an anonymous function use the use keyword. What you doing in you code is just renaming the the second parameter (which is the arguments array, and in this case its empty) to $meta and passing that to the get_post_meta function array.

Try refactoring your code to this.

add_action( 'graphql_register_types', function() {

   $metas = array('phone', 'city', 'state', 'zip');

   foreach( $metas as $meta ) {

       register_graphql_field( 'Location', $meta, [
           'type'        => 'String',
           'description' => __( 'The post data', 'wp-graphql' ),
           'resolve'     => function( $post ) use( $meta ) { // Note use( $meta )
               $GQL_data = get_post_meta( get_the_ID(), $meta, true);
               return ! empty( $GQL_data  ) ? $GQL_data  : 'fail';
           }
       ] );

   }
} );