简体   繁体   English

如何将变量传递给 strapi graphql api?

[英]How to pass variables to strapi graphql api?

I have the following graphql code in my strapi and nextjs project.我的 strapi 和 nextjs 项目中有以下 graphql 代码。 The code is working in the graphql end point playground but some how it keeps getting error in my local environment showing message: 'Syntax Error: Expected Name, found <EOF>.', when I try to fetch data from nextjs.该代码在 graphql 端点游乐场中运行,但在我尝试从 nextjs 获取数据时,它如何在我的本地环境中不断出现错误并显示message: 'Syntax Error: Expected Name, found <EOF>.', I have ready this strapi documentation but cant seems to find anything relevant with my current issues.我已经准备好这份strapi 文档,但似乎找不到与我当前问题相关的任何内容。 Any can please tell me what error I am making here?任何人都可以告诉我我在这里犯了什么错误吗?

export async function getPostAndMorePosts(slug, preview) {
  const data = await fetchAPI(
    `
    fragment FileParts on UploadFileEntityResponse {
      data {
        id
        attributes {
          alternativeText
          width
          height
          mime
          url
          formats
        }
      }
    }
query Posts($slug: String!) {
  posts(filters: {slug: {eq: $slug}})  {
    data{
      attributes{
        title
        content
        
        excerpt
        slug
        category{
          data{
            attributes{
              name
            }
          }
        }

        coverImage{
          ...FileParts

        }
        
      }
    }
  
}
  
  
  morePost: posts(filters: {slug: {ne: $slug}},pagination: { start: 0, limit: 4 })  {
    data{
      attributes{
        title
        content
        
        excerpt
        slug
        category{
          data{
            attributes{
              name
            }
          }
        }

        coverImage{
          ...FileParts

        }
        publishedAt
        
      }
    }
  
  }
}`,
    {variables:{
    
          slug:slug
        }
      }
  )
  return data
}

Your code needs a closing bracket which is misaligned at the moment!您的代码目前需要一个未对齐的右括号! Look at the code below看下面的代码

export async function getPostAndMorePosts(slug, preview) {
    const data = await fetchAPI(
      `
      fragment FileParts on UploadFileEntityResponse {
        data {
          id
          attributes {
            alternativeText
            width
            height
            mime
            url
            formats
          }
        }
      }
    } -> closed here
  query Posts($slug: String!) {
    posts(filters: {slug: {eq: $slug}})  {
      data{
        attributes{
          title
          content
          
          excerpt
          slug
          category{
            data{
              attributes{
                name
              }
            }
          }
  
          coverImage{
            ...FileParts
  
          }
          
        }
      }
    
  }
    
    
    morePost: posts(filters: {slug: {ne: $slug}},pagination: { start: 0, limit: 4 })  {
      data{
        attributes{
          title
          content
          
          excerpt
          slug
          category{
            data{
              attributes{
                name
              }
            }
          }
  
          coverImage{
            ...FileParts
  
          }
          publishedAt
          
        }
      }
    
    }
  `, -> removed from here
      {variables:{
      
            slug:slug
          }
        }
    )
    return data
  }

So it's just an issue of {} alligment.所以这只是 {} 分配的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM